Minimum time required to produce m items

24 Jan, 2022
Consider n machines which produce same type of items but at different rate i.e., machine 1 takes a1 sec to produce an item, machine 2 takes a2 sec to produce an item. Given an array that contains the time required by ith machine to produce an item. Considering all machines are working simultaneously, the task is to find the minimum time required to produce m items.
Examples: 
Input : arr[] = {1, 2, 3}, m = 11
Output : 6
In 6 sec, machine 1 produces 6 items, machine 2 produces 3 items,
and machine 3 produces 2 items. So to produce 11 items minimum
6 sec are required.

Input : arr[] = {5, 6}, m = 11
Output : 30
Method 1 : (Brute Force) Initialize time = 0 and increment it by 1. Calculate number of item produce at each time until number of produced items is not equal to m. 
Below is the implementation of the above idea :
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find minimum time// required to produce m items.#include<bits/stdc++.h>using namespace std; // Return the minimum time required to// produce m items with given machines.int minTime(int arr[], int n, int m){    // Initialize time, items equal to 0.    int t = 0;     while (1)    {        int items = 0;         // Calculating items at each second        for (int i = 0; i < n; i++)            items += (t / arr[i]);         // If items equal to m return time.        if (items >= m)            return t;         t++; // Increment time    }} // Driver Codeint main(){    int arr[] = { 1, 2, 3 };    int n = sizeof(arr)/sizeof(arr[0]);    int m = 11;     cout << minTime(arr, n, m) << endl;     return 0;}
// Java program to find minimum time// required to produce m items.import java.io.*; public class GFG{ // Return the minimum time required to// produce m items with given machines.static int minTime(int []arr, int n,                   int m){         // Initialize time, items equal to 0.    int t = 0;     while (true)    {        int items = 0;         // Calculating items at each second        for (int i = 0; i < n; i++)            items += (t / arr[i]);         // If items equal to m return time.        if (items >= m)            return t;         t++; // Increment time    }}     // Driver Code    static public void main (String[] args)    {            int []arr = { 1, 2, 3 };            int n = arr.length;            int m = 11;     System.out.println(minTime(arr, n, m));    }} // This code is contributed by vt_m.
# Python3 program to find minimum time# required to produce m items.import math as mt # Return the minimum time required to# produce m items with given machines.def minTime(arr, n, m):     # Initialize time, items equal to 0.    t = 0     while (1):             items = 0         # Calculating items at each second        for i in range(n):            items += (t // arr[i])         # If items equal to m return time.        if (items >= m):            return t         t += 1 # Increment time     # Driver Codearr = [1, 2, 3]n = len(arr)m = 11 print(minTime(arr, n, m) ) # This code is contributed by# Mohit kumar 29
// C# program to find minimum time// required to produce m items.using System;  public class GFG{  // Return the minimum time// required to produce m// items with given machines.static int minTime(int []arr, int n,                   int m){         // Initialize time, items equal to 0.    int t = 0;      while (true)    {        int items = 0;          // Calculating items at each second        for (int i = 0; i < n; i++)            items += (t / arr[i]);          // If items equal to m return time.        if (items >= m)            return t;          t++; // Increment time    }}  // Driven Codestatic public void Main (String []args){    int []arr = {1, 2, 3};    int n = arr.Length;    int m = 11;      // Calling function    Console.WriteLine(minTime(arr, n, m));     }}
<?php// PHP program to find minimum time// required to produce m items. // Return the minimum time required to// produce m items with given machines.function minTime($arr, $n, $m){         // Initialize time, items    // equal to 0.    $t = 0;     while (1)    {        $items = 0;         // Calculating items at each second        for ( $i = 0; $i < $n; $i++)            $items += ($t / $arr[$i]);         // If items equal to m return time.        if ($items >= $m)            return $t;         $t++; // Increment time    }}     // Driver Code    $arr = array(1, 2, 3);    $n =count($arr);    $m = 11;    echo minTime($arr, $n, $m); // This code is contributed by anuj_67.?>
<script> // JavaScript program to find minimum time// required to produce m items. // Return the minimum time required to// produce m items with given machines.function minTime(arr, n, m){          // Initialize time, items equal to 0.    let t = 0;      while (true)    {        let items = 0;          // Calculating items at each second        for (let i = 0; i < n; i++)            items += (t / arr[i]);          // If items equal to m return time.        if (items >= m)            return t;          t++; // Increment time    }} // Driver Code    let arr = [ 1, 2, 3 ];    let n = arr.length;    let m = 11;      document.write(minTime(arr, n, m)); </script>
Output:  
6
Method 2 (efficient): The idea is to use Binary Search. Maximum possible time required to produce m items will be maximum time in the array, amax, multiplied by m i.e amax * m. So, use binary search between 1 to amax * m and find the minimum time which produce m items.
 Below is the implementation of the above idea :
C++
Java
Python3
C#
PHP
Javascript
// Efficient C++ program to find minimum time// required to produce m items.#include<bits/stdc++.h>using namespace std; // Return the number of items can be// produced in temp sec.int findItems(int arr[], int n, int temp){    int ans = 0;    for (int i = 0; i < n; i++)        ans += (temp/arr[i]);    return ans;} // Binary search to find minimum time required// to produce M items.int bs(int arr[], int n, int m, int high){    int low = 1;     // Doing binary search to find minimum    // time.    while (low < high)    {        // Finding the middle value.        int mid = (low+high)>>1;         // Calculate number of items to        // be produce in mid sec.        int itm = findItems(arr, n, mid);         // If items produce is less than        // required, set low = mid + 1.        if (itm < m)            low = mid+1;         //  Else set high = mid.        else            high = mid;    }     return high;} // Return the minimum time required to// produce m items with given machine.int minTime(int arr[], int n, int m){    int maxval = INT_MIN;     // Finding the maximum time in the array.    for (int i = 0; i < n; i++)        maxval = max(maxval, arr[i]);     return bs(arr, n, m, maxval*m);} // Driven Programint main(){    int arr[] = { 1, 2, 3 };    int n = sizeof(arr)/sizeof(arr[0]);    int m = 11;     cout << minTime(arr, n, m) << endl;     return 0;}
// Efficient Java program to find// minimum time required to// produce m items.import java.io.*; public class GFG{     // Return the number of items can// be produced in temp sec.static int findItems(int []arr, int n,                     int temp){    int ans = 0;    for (int i = 0; i < n; i++)        ans += (temp / arr[i]);    return ans;} // Binary search to find minimum time// required to produce M items.static int bs(int []arr, int n,              int m, int high)              {    int low = 1;     // Doing binary search to    // find minimum time.    while (low < high)    {        // Finding the middle value.        int mid = (low + high) >> 1;         // Calculate number of items to        // be produce in mid sec.        int itm = findItems(arr, n, mid);         // If items produce is less than        // required, set low = mid + 1.        if (itm < m)            low = mid + 1;         // Else set high = mid.        else            high = mid;    }     return high;} // Return the minimum time required to// produce m items with given machine.static int minTime(int []arr, int n,                   int m){    int maxval = Integer.MIN_VALUE;     // Finding the maximum time in the array.    for (int i = 0; i < n; i++)        maxval = Math.max(maxval, arr[i]);     return bs(arr, n, m, maxval * m);} // Driven Programstatic public void main (String[] args){    int []arr = {1, 2, 3};        int n = arr.length;    int m = 11;     System.out.println(minTime(arr, n, m));}} // This code is contributed by vt_m.
# Efficient Python3 program to find# minimum time required to produce m items.import sys def findItems(arr, n, temp):    ans = 0    for i in range(n):        ans += temp // arr[i]    return ans # Binary search to find minimum time# required to produce M items.def bs(arr, n, m, high):    low = 1     # Doing binary search to find minimum    # time.    while low < high:         # Finding the middle value.        mid = (low + high) >> 1         # Calculate number of items to        # be produce in mid sec.        itm = findItems(arr, n, mid)         # If items produce is less than        # required, set low = mid + 1.        if itm < m:            low = mid + 1         # Else set high = mid.        else:            high = mid    return high # Return the minimum time required to# produce m items with given machine.def minTime(arr, n, m):    maxval = -sys.maxsize     # Finding the maximum time in the array.    for i in range(n):        maxval = max(maxval, arr[i])     return bs(arr, n, m, maxval * m) # Driver Codeif __name__ == "__main__":    arr = [1, 2, 3]    n = len(arr)    m = 11    print(minTime(arr, n, m)) # This code is contributed by# sanjeev2552
// Efficient C# program to find// minimum time required to// produce m items.using System; public class GFG{     // Return the number of items can// be produced in temp sec.static int findItems(int []arr, int n,                     int temp){    int ans = 0;    for (int i = 0; i < n; i++)        ans += (temp / arr[i]);    return ans;} // Binary search to find minimum time// required to produce M items..static int bs(int []arr, int n,              int m, int high){    int low = 1;     // Doing binary search to    // find minimum time.    while (low < high)    {        // Finding the middle value.        int mid = (low + high) >> 1;         // Calculate number of items to        // be produce in mid sec.        int itm = findItems(arr, n, mid);         // If items produce is less than        // required, set low = mid + 1.        if (itm < m)            low = mid + 1;         // Else set high = mid.        else            high = mid;    }     return high;} // Return the minimum time required to// produce m items with given machine.static int minTime(int []arr, int n,                   int m){    int maxval = int.MinValue;     // Finding the maximum time in the array.    for (int i = 0; i < n; i++)        maxval = Math.Max(maxval, arr[i]);     return bs(arr, n, m, maxval * m);} // Driver Codestatic public void Main (){    int []arr = {1, 2, 3};    int n = arr.Length;    int m = 11;     Console.WriteLine(minTime(arr, n, m));}} // This code is contributed by vt_m.
<?php// Efficient PHP program to find minimum// time required to produce m items. // Return the number of items can be// produced in temp sec.function findItems( $arr, $n, $temp){    $ans = 0;    for($i = 0; $i < $n; $i++)        $ans += ($temp / $arr[$i]);    return $ans;} // Binary search to find minimum// time required to produce M items.function bs($arr, $n, $m, $high){    $low = 1;     // Doing binary search to    // find minimum time.    while ($low < $high)    {                 // Finding the middle value.        $mid = ($low + $high) >> 1;         // Calculate number of items to        // be produce in mid sec.        $itm = findItems($arr, $n, $mid);         // If items produce is less than        // required, set low = mid + 1.        if ($itm < $m)            $low = $mid + 1;         // Else set high = mid.        else            $high = $mid;    }     return $high;} // Return the minimum time required to// produce m items with given machine.function minTime($arr, $n, $m){    $maxval = PHP_INT_MIN;     // Finding the maximum    // time in the array.    for($i = 0; $i < $n; $i++)        $maxval = max($maxval, $arr[$i]);     return bs($arr, $n, $m, $maxval*$m);}     // Driver Code    $arr = array(1, 2, 3);    $n = count($arr);    $m = 11;     echo minTime($arr, $n, $m); // This code is contributed by anuj_67.?>
<script>// Efficient Javascript program to find// minimum time required to// produce m items. // Return the number of items can// be produced in temp sec.function findItems(arr,n,temp){    let ans = 0;    for (let i = 0; i < n; i++)        ans += Math.floor(temp / arr[i]);    return ans;} // Binary search to find minimum time// required to produce M items.function bs(arr,n,m,high){    let low = 1;      // Doing binary search to    // find minimum time.    while (low < high)    {        // Finding the middle value.        let mid = (low + high) >> 1;          // Calculate number of items to        // be produce in mid sec.        let itm = findItems(arr, n, mid);          // If items produce is less than        // required, set low = mid + 1.        if (itm < m)            low = mid + 1;          // Else set high = mid.        else            high = mid;    }      return high;} // Return the minimum time required to// produce m items with given machine.function minTime(arr,n,m){    let maxval = Number.MIN_VALUE;      // Finding the maximum time in the array.    for (let i = 0; i < n; i++)        maxval = Math.max(maxval, arr[i]);      return bs(arr, n, m, maxval * m);}   // Driven Programlet arr=[1, 2, 3];let n = arr.length;let m = 11;document.write(minTime(arr, n, m));      // This code is contributed by patel2127</script>
Output:  
6
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
mohit kumar 29
sanjeev2552
samraj
souravghosh0416
patel2127
anikaseth98
as5853535
surindertarika1234
amartyaghoshgfg
Binary Search
Searching
Searching
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.