QUESTION
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 which contains the time required by ith machine to produce an item. Considering all machine are working simultaneously, the task is to find minimum time required to produce m items.
“TESTCASE_1”: “3\n1 2 3\n11\n###—###SEPERATOR—###—\n6”, “TESTCASE_2”: “2\n5 6\n11\n###—###SEPERATOR—###—\n30”, “TESTCASE_3”: “7\n3 7 10 19 38 58 69\n5\n###—###SEPERATOR—###—\n10”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0
ANSWER
#include <iostream>
using namespace std;
// Return the minimum time required to
// produce m items with given machines.
int minTime(int arr[], int n, int m)
{
// Intialise 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
}
}
// Driven Program
int main()
{
int n,m;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cin>>m;
cout << minTime(arr, n, m) << endl;
return 0;
}