Open In App

Minimum time required to produce m items

Last Updated : 16 Mar, 2025
Comments
Improve
Suggest changes
27 Likes
Like
Report

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:

  • Machine 1 produces an item every 2 seconds → Produces 8/2 = 4 items in 8 seconds.
  • Machine 2 produces an item every 4 seconds → Produces 8/4 = 2 items in 8 seconds.
  • Machine 3 produces an item every 5 seconds → Produces 8/5 = 1 item in 8 seconds.

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:

  • Machine 1 produces an item every 2 seconds - Produces 9/2 = 4 items in 9 seconds.
  • Machine 2 produces an item every 3 seconds - Produces 9/3 = 3 items in 9 seconds.
  • Machine 3 produces an item every 5 seconds - Produces 9/5 = 1 item in 9 seconds.
  • Machine 4 produces an item every 7 seconds - Produces 9/7 = 1 item in 9 seconds.

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

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<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.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));

Output
8

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:

  • Set left to 1 and right to m * minMachineTime to define the search space.
  • Initialize ans with right to store the minimum time required.
  • Run binary search while left is less than or equal to right.
  • Calculate mid and compute totalItems by iterating through arr and summing up mid / arr[i].
  • If totalItems is at least m, update ans and search for a smaller time. Otherwise, adjust left to mid + 1 for a larger time.
  • Continue searching until the optimal minimum time is found.
C++
// 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<int> &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));

Output
8

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.
 


Article Tags :

Explore