CSES Solutions - Factory Machines
Last Updated :
14 Oct, 2024
A factory has N machines which can be used to make products. Your goal is to make a total of T products.
For each machine, you know the number of seconds it needs to make a single product, given as array K[]. The machines can work simultaneously, and you can freely decide their schedule.
What is the shortest time needed to make T products?
Examples:
Input: N = 3, T = 7, K[] = {3, 2, 5}
Output: 8
Explanation:
- Machine 1 will make 2 products. Total time taken = 3 * 2 = 6 seconds.
- Machine 2 will make 4 products. Total time taken = 2 * 4 = 8 seconds.
- Machine 3 will make 1 product. Total time taken = 5 * 1 = 5 seconds.
Shortest time to make 7 products = 8 seconds.
Input: N = 2, T = 5, K[] = {1, 2}
Output: 4
Explanation:
- Machine 1 will make 3 products. Total time taken = 1 * 3 = 3 seconds.
- Machine 2 will make 2 products. Total time taken = 2 * 2 = 4 seconds
Shortest time to make 5 products = 4 seconds.
Approach: To solve the problem, follow the below idea:
The problem can be solved using Binary Search on Answer. We can binary search for the time needed to make the T products. If we observe carefully, the time to make T products will be at least 1 second and the maximum time to make T products will be when we have only 1 machine and it takes maximum amount of time to make one product. So, we can initialize low = 1 and high = T * min value in K[]. Now, we can find mid and find the total number of products we can making using all machines. If the total number of products is less than T, then it means we need more time so we will shift lo = mid + 1. Otherwise, we shift high = mid - 1 and update the answer. We keep on searching till low <= high and after all the iterations, we can return the final answer.
Step-by-step algorithm:
- Maintain a function check(mid, N, T, K) to check whether it is possible to make T products in time <= mid.
- Also maintain a variable ans to store the minimum time to make T products.
- Declare the range in which our answer can lie, that is low = 1, high = T * min value in K[].
- Iterate till low <= high,
- Find the mid in range [low, high] and check if we can make T products in time <= mid.
- If we can make T products in time <= mid, then update the answer and reduce search space by moving high to mid - 1.
- Otherwise, move low = mid + 1.
- After all the iterations, return ans as the final answer.
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
// Make the check function to check if we can make T
// products in time <= mid
bool check(ll mid, ll N, ll T, ll* K)
{
// Variable to count the number of products made
ll sum = 0;
for (int i = 0; i < N; i++) {
sum += (mid / K[i]);
if (sum >= T)
return true;
}
return false;
}
// Function to find the minimum time to make T products
ll solve(ll N, ll T, ll* K)
{
// Define the range in which our answer can lie
ll low = 1, high = (*min_element(K, K + N)) * T ;
ll ans;
// Binary Search for the minimum time to make T products
while (low <= high) {
ll mid = (low + high) / 2;
// Check if we can make T products in time <= mid
if (check(mid, N, T, K)) {
// Update the answer and reduce search space by
// moving high to mid - 1
ans = mid;
high = mid - 1;
}
else {
// Reduce the search space by moving low to mid
// + 1
low = mid + 1;
}
}
return ans;
}
int main()
{
// Sample Input
ll N = 3, T = 7;
ll K[] = { 3, 2, 5 };
cout << solve(N, T, K);
}
Java
import java.util.Arrays;
public class Main {
// Function to check if we can make T products in time <= mid
static boolean check(long mid, long N, long T, long[] K) {
// Variable to count the number of products made
long sum = 0;
for (int i = 0; i < N; i++) {
sum += (mid / K[i]);
if (sum >= T)
return true;
}
return false;
}
// Function to find the minimum time to make T products
static long solve(long N, long T, long[] K) {
// Define the range in which our answer can lie
long low = 1, high = Arrays.stream(K).min().getAsLong() * T; // Updated high value
long ans = 0;
// Binary Search for the minimum time to make T products
while (low <= high) {
long mid = (low + high) / 2;
// Check if we can make T products in time <= mid
if (check(mid, N, T, K)) {
// Update the answer and reduce search space by
// moving high to mid - 1
ans = mid;
high = mid - 1;
} else {
// Reduce the search space by moving low to mid + 1
low = mid + 1;
}
}
return ans;
}
public static void main(String[] args) {
// Sample Input
long N = 3, T = 7;
long[] K = { 3, 2, 5 };
System.out.println(solve(N, T, K));
}
}
// This code is contributed by shivamgupta0987654321
Python
def check(mid, N, T, K):
# Variable to count the number of products made
sum_val = 0
for i in range(N):
sum_val += (mid // K[i])
if sum_val >= T:
return True
return False
def solve(N, T, K):
# Define the range in which our answer can lie
low, high = 1, min(K) * T
ans = 0
# Binary Search for the minimum time to make T products
while low <= high:
mid = (low + high) // 2
# Check if we can make T products in time <= mid
if check(mid, N, T, K):
# Update the answer and reduce search space by moving high to mid - 1
ans = mid
high = mid - 1
else:
# Reduce the search space by moving low to mid + 1
low = mid + 1
return ans
if __name__ == "__main__":
# Sample Input
N = 3
T = 7
K = [3, 2, 5]
# Output the result
print(solve(N, T, K))
C#
using System;
using System.Linq;
class Program
{
// Function to check if we can make T products in time <= mid
static bool Check(long mid, long N, long T, long[] K)
{
// Variable to count the number of products made
long sum = 0;
for (int i = 0; i < N; i++)
{
sum += (mid / K[i]);
if (sum >= T)
return true;
}
return false;
}
// Function to find the minimum time to make T products
static long Solve(long N, long T, long[] K)
{
// Define the range in which our answer can lie
long low = 1, high = K.Min() * T;
long ans = 0;
// Binary Search for the minimum time to make T products
while (low <= high)
{
long mid = (low + high) / 2;
// Check if we can make T products in time <= mid
if (Check(mid, N, T, K))
{
// Update the answer and reduce search space by
// moving high to mid - 1
ans = mid;
high = mid - 1;
}
else
{
// Reduce the search space by moving low to mid + 1
low = mid + 1;
}
}
return ans;
}
static void Main(string[] args)
{
// Sample Input
long N = 3, T = 7;
long[] K = { 3, 2, 5 };
Console.WriteLine(Solve(N, T, K));
}
}
JavaScript
// Make the check function to check if we can make T
// products in time <= mid
function check(mid, N, T, K) {
// Variable to count the number of products made
let sum = 0;
for (let i = 0; i < N; i++) {
sum += Math.floor(mid / K[i]);
if (sum >= T)
return true;
}
return false;
}
// Function to find the minimum time to make T products
function solve(N, T, K) {
// Define the range in which our answer can lie
let low = 1, high = Math.min(...K) * T;
let ans;
// Binary Search for the minimum time to make T products
while (low <= high) {
let mid = Math.floor((low + high) / 2);
// Check if we can make T products in time <= mid
if (check(mid, N, T, K)) {
// Update the answer and reduce search space by
// moving high to mid - 1
ans = mid;
high = mid - 1;
} else {
// Reduce the search space by moving low to mid
// + 1
low = mid + 1;
}
}
return ans;
}
// Sample Input
let N = 3, T = 7;
let K = [3, 2, 5];
console.log(solve(N, T, K));
// This code is contributed by Ayush Mishra
Time Complexity: O(N * log(T * max(K[]))), N is the number of machines, T is the number of products and K[] is the array of time which each machine takes to make 1 product.
Auxiliary Space: O(1)
Similar Reads
CSES Problem Set Solutions
In this article, we have compiled comprehensive, high-quality tutorials on the CSES Problem Set Solutions to assist you in understanding the problem set for learning algorithmic programming. What is CSES Problem Set?CSES Problem Set is a collection of competitive programming tasks hosted on the CSES
8 min read
Types of Electric Machines
Electric machines are essential devices in electric engineering that convert electric power into mechanical electricity or vice versa. These machines play a vital position in diverse programs, from powering business gadgets to propelling electric-powered vehicles. The 3 number one sorts of electric-
14 min read
Use Cases of Computer Vision in Manufacturing
In this rapidly evolving landscape of manufacturing, leveraging advanced technologies is crucial for maintaining a competitive edge. One such technology, computer vision, is transforming the industry by automating and optimizing various processes. Use Cases of Computer Vision in ManufacturingThis ar
6 min read
Production Function: Meaning, Features, and Types
What is Production Function?Production Function is the relationship between physical inputs (land, labour, capital, etc.) and physical outputs (quantity produced). It is a technical relationship (not an economic relationship) that studies material inputs on one hand and material outputs on the other
6 min read
The Factory Pattern in Scala
The factory method is used to offer a single interface to instantiate one of the multiple classes. In the Factory pattern, the objective is to make the object in such a way that it does not expose to the creation logic to the Client and can always refer to a newly created object with the help of a c
3 min read
Solutions of Chapter-2: Sectors of the Indian Economy
NCERT Solutions For Class 10 Economics Chapter-2 Sectors of the Indian Economyâ This article includes free NCERT Solutions For Class 10 Economics Chapter 2 Sectors of the Indian Economy to help students of Class 10 learn the solutions and ace their exams. It has been developed by the subject matter
15+ min read
Vending Machine - Low Level Design
Vending machines have become an essential part of our everyday lives, offering various kinds of products starting from snacks and beverages to personal care items. While their capability can also appear simple from a user perspective, the low-level design of a vending machine includes complex info t
8 min read
NCERT Solutions for Class 10 Geography Chapter 6 : Manufacturing Industries
This article will help the students to deal with questions related to NCERT Â Solutions for Class 10 Geography Chapter 6: Manufacturing Industries. The manufacturing sector is regarded as the backbone of development and indicates the growth and strength of a country. There are a number of important m
6 min read
NCERT Solutions For Class-12 Geography Chapter-5: Secondary Activities
NCERT Solutions For Class Class-12 Geography Chapter-5 Secondary Activitiesâ This article includes free NCERT Solutions For Class Class 12 Geography Chapter 5 Secondary Activities to help students of Class 12 learn the solutions and ace their exams.It has been developed by the subject matter experts
9 min read
Solutions of Chapter-6 Manufacturing Industries: Class-10 Geography
NCERT Solutions For Class 10 Geography Chapter 6 Manufacturing Industriesâ This article includes free NCERT Solutions For Class 10 Geography Chapter 6 Manufacturing Industries to help students of Class 10 learn the solutions and ace their exams.It has been developed by the subject matter experts at
5 min read