Minimum time required to produce m items (original) (raw)

Last Updated : 16 Mar, 2025

Given **n machines represented by an integer array arr[], where **arr[i] denotes the time (in seconds) taken by the **i-th machine to produce **one item. All machines work **simultaneously and continuously. Additionally, we are also given an integer **m, representing the total number of **items required. The task is to determine the **minimum time needed to produce exactly **m items efficiently.

**Examples:

**Input: arr[] = [2, 4, 5], m = 7
**Output: 8
**Explanation: The optimal way to produce **7 items in the **minimum time is **8 seconds. Each machine produces items at different rates:

Total items produced in **8 seconds = **4 + 2 + 1 = 7

**Input: arr[] = [2, 3, 5, 7], m = 10
**Output: 9
**Explanation: The optimal way to produce **10 items in the **minimum time is **9 seconds. Each machine produces items at different rates:

Total items produced in **9 seconds = **4 + 3 + 1 + 1 = 10

Table of Content

Using Brute Force Method - O(n*m*min(arr)) Time and O(1) Space

The idea is to **incrementally check the minimum time required to produce exactly **m items. We start with **time = 1 and keep increasing it until the total items produced by all machines **≥ m. At each time step, we compute the number of items each machine can produce using **time / arr[i] and sum them up. Since all machines work **simultaneously, this approach ensures we find the smallest valid time.

C++ `

// C++ program to find minimum time // required to produce m items using // Brute Force Approach #include <bits/stdc++.h> using namespace std;

int minTimeReq(vector &arr, int m) {

// Start checking from time = 1
int time = 1;

while (true) {
    int totalItems = 0;

    // Calculate total items produced at 
    // current time
    for (int i = 0; i < arr.size(); i++) {
        totalItems += time / arr[i];
    }

    // If we produce at least m items, 
    // return the time
    if (totalItems >= m) {
        return time;
    }

    // Otherwise, increment time and 
    // continue checking
    time++;
}

}

int main() {

vector<int> arr = {2, 4, 5};
int m = 7;

cout << minTimeReq(arr, m) << endl;

return 0;

}

Java

// Java program to find minimum time // required to produce m items using // Brute Force Approach import java.util.*;

class GfG {

static int minTimeReq(int arr[], int m) {
    
    // Start checking from time = 1
    int time = 1;
    
    while (true) {
        int totalItems = 0;

        // Calculate total items produced at 
        // current time
        for (int i = 0; i < arr.length; i++) {
            totalItems += time / arr[i];
        }

        // If we produce at least m items, 
        // return the time
        if (totalItems >= m) {
            return time;
        }

        // Otherwise, increment time and 
        // continue checking
        time++;
    }
}

public static void main(String[] args) {
    
    int arr[] = {2, 4, 5};
    int m = 7;

    System.out.println(minTimeReq(arr, m));
}

}

Python

Python program to find minimum time

required to produce m items using

Brute Force Approach

def minTimeReq(arr, m):

# Start checking from time = 1
time = 1

while True:
    totalItems = 0

    # Calculate total items produced at 
    # current time
    for i in range(len(arr)):
        totalItems += time // arr[i]

    # If we produce at least m items, 
    # return the time
    if totalItems >= m:
        return time

    # Otherwise, increment time and 
    # continue checking
    time += 1

if name == "main":

arr = [2, 4, 5]
m = 7

print(minTimeReq(arr, m))

C#

// C# program to find minimum time // required to produce m items using // Brute Force Approach using System;

class GfG {

static int minTimeReq(int[] arr, int m) {
    
    // Start checking from time = 1
    int time = 1;
    
    while (true) {
        int totalItems = 0;

        // Calculate total items produced at 
        // current time
        for (int i = 0; i < arr.Length; i++) {
            totalItems += time / arr[i];
        }

        // If we produce at least m items, 
        // return the time
        if (totalItems >= m) {
            return time;
        }

        // Otherwise, increment time and 
        // continue checking
        time++;
    }
}

public static void Main() {
    
    int[] arr = {2, 4, 5};
    int m = 7;

    Console.WriteLine(minTimeReq(arr, m));
}

}

JavaScript

// JavaScript program to find minimum time // required to produce m items using // Brute Force Approach

function minTimeReq(arr, m) {

// Start checking from time = 1
let time = 1;

while (true) {
    let totalItems = 0;

    // Calculate total items produced at 
    // current time
    for (let i = 0; i < arr.length; i++) {
        totalItems += Math.floor(time / arr[i]);
    }

    // If we produce at least m items, 
    // return the time
    if (totalItems >= m) {
        return time;
    }

    // Otherwise, increment time and 
    // continue checking
    time++;
}

}

// Input values let arr = [2, 4, 5]; let m = 7;

console.log(minTimeReq(arr, m));

`

**Time Complexity: O(n*m*min(arr)) because for each time unit (up to m * min(arr)), we iterate through n machines to count produced items.
**Space Complexity: O(1), as only a few integer variables are used; no extra space is allocated.

Using Binary Search - O(n*log(m*min(arr))) Time and O(1) Space

The idea is to use **Binary Search instead of checking each time **sequentially, we observe that the total items produced in a given time **T can be computed in **O(n). The key observation is that the minimum possible time is **1, and the maximum possible time is **m * minMachineTime. By applying **binary search on this range, we repeatedly check the mid value to determine if it's sufficient and adjust the search space accordingly.

Steps to implement the above idea:

// C++ program to find minimum time // required to produce m items using // Binary Search Approach #include <bits/stdc++.h> using namespace std;

int minTimeReq(vector &arr, int m) {

// Find the minimum value in arr manually
int minMachineTime = arr[0];
for (int i = 1; i < arr.size(); i++) {
    if (arr[i] < minMachineTime) {
        minMachineTime = arr[i];
    }
}

// Define the search space
int left = 1;
int right = m * minMachineTime;
int ans = right;

while (left <= right) {
    
    // Calculate mid time
    int mid = left + (right - left) / 2;
    int totalItems = 0;

    // Calculate total items produced in 'mid' time
    for (int i = 0; i < arr.size(); i++) {
        totalItems += mid / arr[i];
    }

    // If we can produce at least m items,
    // update answer
    if (totalItems >= m) {
        ans = mid;
        
        // Search for smaller time
        right = mid - 1;
    } 
    else {
        
        // Search in right half
        left = mid + 1;
    }
}

return ans;

}

int main() {

vector<int> arr = {2, 4, 5};
int m = 7;

cout << minTimeReq(arr, m) << endl;

return 0;

}

Java

// Java program to find minimum time // required to produce m items using // Binary Search Approach import java.util.*;

class GfG {

static int minTimeReq(int[] arr, int m) {
    
    // Find the minimum value in arr manually
    int minMachineTime = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] < minMachineTime) {
            minMachineTime = arr[i];
        }
    }

    // Define the search space
    int left = 1;
    int right = m * minMachineTime;
    int ans = right;
    
    while (left <= right) {
        
        // Calculate mid time
        int mid = left + (right - left) / 2;
        int totalItems = 0;

        // Calculate total items produced in 'mid' time
        for (int i = 0; i < arr.length; i++) {
            totalItems += mid / arr[i];
        }

        // If we can produce at least m items,
        // update answer
        if (totalItems >= m) {
            ans = mid;
            
            // Search for smaller time
            right = mid - 1;
        } 
        else {
            
            // Search in right half
            left = mid + 1;
        }
    }
    
    return ans;
}

public static void main(String[] args) {
    
    int[] arr = {2, 4, 5};
    int m = 7;

    System.out.println(minTimeReq(arr, m));
}

}

Python

Python program to find minimum time

required to produce m items using

Binary Search Approach

def minTimeReq(arr, m):

# Find the minimum value in arr manually
minMachineTime = arr[0]
for i in range(1, len(arr)):
    if arr[i] < minMachineTime:
        minMachineTime = arr[i]

# Define the search space
left = 1
right = m * minMachineTime
ans = right

while left <= right:
    
    # Calculate mid time
    mid = left + (right - left) // 2
    totalItems = 0

    # Calculate total items produced in 'mid' time
    for i in range(len(arr)):
        totalItems += mid // arr[i]

    # If we can produce at least m items,
    # update answer
    if totalItems >= m:
        ans = mid
        
        # Search for smaller time
        right = mid - 1
    else:
        
        # Search in right half
        left = mid + 1

return ans

if name == "main":

arr = [2, 4, 5]
m = 7

print(minTimeReq(arr, m))

C#

// C# program to find minimum time // required to produce m items using // Binary Search Approach using System;

class GfG {

static int minTimeReq(int[] arr, int m) {
    
    // Find the minimum value in arr manually
    int minMachineTime = arr[0];
    for (int i = 1; i < arr.Length; i++) {
        if (arr[i] < minMachineTime) {
            minMachineTime = arr[i];
        }
    }

    // Define the search space
    int left = 1;
    int right = m * minMachineTime;
    int ans = right;
    
    while (left <= right) {
        
        // Calculate mid time
        int mid = left + (right - left) / 2;
        int totalItems = 0;

        // Calculate total items produced in 'mid' time
        for (int i = 0; i < arr.Length; i++) {
            totalItems += mid / arr[i];
        }

        // If we can produce at least m items,
        // update answer
        if (totalItems >= m) {
            ans = mid;
            
            // Search for smaller time
            right = mid - 1;
        } 
        else {
            
            // Search in right half
            left = mid + 1;
        }
    }
    
    return ans;
}

static void Main() {
    
    int[] arr = {2, 4, 5};
    int m = 7;

    Console.WriteLine(minTimeReq(arr, m));
}

}

JavaScript

// JavaScript program to find minimum time // required to produce m items using // Binary Search Approach

function minTimeReq(arr, m) {

// Find the minimum value in arr manually
let minMachineTime = arr[0];
for (let i = 1; i < arr.length; i++) {
    if (arr[i] < minMachineTime) {
        minMachineTime = arr[i];
    }
}

// Define the search space
let left = 1;
let right = m * minMachineTime;
let ans = right;

while (left <= right) {
    
    // Calculate mid time
    let mid = Math.floor(left + (right - left) / 2);
    let totalItems = 0;

    // Calculate total items produced in 'mid' time
    for (let i = 0; i < arr.length; i++) {
        totalItems += Math.floor(mid / arr[i]);
    }

    // If we can produce at least m items,
    // update answer
    if (totalItems >= m) {
        ans = mid;
        
        // Search for smaller time
        right = mid - 1;
    } 
    else {
        
        // Search in right half
        left = mid + 1;
    }
}

return ans;

}

// Driver code let arr = [2, 4, 5]; let m = 7;

console.log(minTimeReq(arr, m));

`

**Time Complexity: O(n log(m*min(arr))), as Binary Search runs log(m × min(arr)) times, each checking n machines.
**Space Complexity: O(1), as only a few extra variables are used, making it constant space.