# Find minimum time to finish all jobs with given constraints in C++

## Concept

With respect of a given array of jobs with different time requirements, there exists k identical assignees available and we are also provided how much time an assignee consumesto do one unit of the job. Our task is to determine the minimum time to complete all jobs with following constraints.

• The first constraint is that an assignee can be assigned only contiguous jobs.

Here, for example, an assignee can be assigned jobs at position 1 and 2, but not at position 3, in an array.

• The second constraint is that two assignees cannot share (or co-assigned) a job, that means, a job cannot be partially assigned to one assignee and partially to other.

## Input

k − Indcates number of assignees available.

t − Indicates time taken by an assignee to finish one unit of job

JOB[] − Indicates an array that represents time requirements of different jobs.

## Input

k = 2, t = 4, JOB[] = {5, 6, 12}

## Output

48

Here, the minimum time required to complete all the jobs is 48.

There are 2 assignees available. We obtain this time by assigning {5, 6} to first assignee and {12} to second assignee.

## Input

k = 4, t = 5, JOB[] = {12, 6, 9, 15, 5, 9}

## Output

75

We get this time by assigning {12} {6, 9} {15} and {5, 9}

## Method

Now the concept is to implement Binary Search. Assume if we have a function (say isPossible()) that indicates us if it’s possible to complete all jobs within a given time and number ofavailable assignees. Here, we are able to solve this problem by doing a binary search forthe answer. It has been seen that if the middle point of binary search is not possible, then search in second half, otherwise search in first half. The lower bound for Binary Search for smallest time can be set as 0. Here, the upper bound can be obtained by addingall provided job times.

At present the question is arisen that how to implement isPossible(). We can implement this function using Greedy Approach. Because we want to know if it is possible to finish all jobs within a given time, we visit through all jobs and maintain assigning jobs to current assignee one by one. At the same time, we should remember that a job can be assigned within the given time limit. At that moment, when time taken by current assignee crosses the provided time, generate a new assignee and begin assigning jobs to it. It has been seen that if the number of assignees becomes more than k, then return false, otherwise return true.

## Example

Live Demo

// C++ program to find minimum time to finish all jobs with
// given number of assignees
#include<bits/stdc++.h>
using namespace std;
// Shows utility function to get maximum element in job1[0..n1-1]
int getMax(int arr1[], int n1){
int result1 = arr1;
for (int i=1; i<n1; i++)
if (arr1[i] > result1)
result1 = arr1[i];
return result1;
}
// Now returns true if it is possible to finish jobs1[] within
// given time 'time1'
bool isPossible(int time1, int K1, int job1[], int n1){
// Here, cnt1 is count of current assignees required for jobs
int cnt1 = 1;
int curr_time1 = 0; // Indicates time assigned to current
//assignee
for (int i = 0; i < n1;){
// Now if time assigned to current assignee exceeds max1,
// increment count1 of assignees.
if (curr_time1 + job1[i] > time1) {
curr_time1 = 0;
cnt1++;
}
else { // Else add time of job to current time and move
// to next job.
curr_time1 += job1[i];
i++;
}
}
//Now returns true if count is smaller than k
return (cnt1 <= K1);
}
// Here, returns minimum time required to finish given array of
//jobs
// K1 --> number of assignees
// T1 --> Time required by every assignee to finish 1 unit
// n1 --> Number of jobs
int findMinTime(int K1, int T1, int job1[], int n1){
// Used to set start and end for binary search
// end provides an upper limit on time1
int end1 = 0, start1 = 0;
for (int i = 0; i < n1; ++i)
end1 += job1[i];
int ans1 = end1; // Used to initialize answer
// Determine the job that takes maximum time
int job_max1 = getMax(job1, n1);
// Perform binary search for minimum feasible time
while (start1 <= end1){
int mid1 = (start1 + end1) / 2;
// Now if it is possible to complete jobs in mid time
if (mid1 >= job_max1 && isPossible(mid1, K1, job1, n1)){
ans1 = min(ans1, mid1); // Used to update answer
end1 = mid1 - 1;
}
else
start1 = mid1 + 1;
}
return (ans1 * T1);
}
// Driver program
int main(){
int job1[] = {12, 6, 9, 15, 5, 9};
// int job1[] = {5, 6, 12};
int n1 = sizeof(job1)/sizeof(job1);
int k1=4, T1=5;
// int k1=2, T1=4;
cout << findMinTime(k1, T1, job1, n1) << endl;
return 0;
}

## Output

75