Given an array **A** and an integer **H** where and . Each element **A[i]** represents remaining pending jobs to be done and **H** represents **hours left** to complete all of the jobs. The task is to find the minimum speed in jobs per Hour at which person needs to work to complete all jobs in **H** hours.

**Note:** If **A[i]** has less job to be done than **speed of person** then he finish all of the work of A[i] and won’t go to next element during this hour.

**Examples**:

Input: A[] = [3, 6, 7, 11], H = 8 Output: 4 Input: A[] = [30, 11, 23, 4, 20], H = 5 Output: 30

**Approach:** If the person can finish all the jobs (within **H** hours) with an speed of **K** jobs/hour then he can finish all jobs with a larger speed too.

If we let **ispossible(K)** be **true** if and only if the person can finish with a job speed of **K**, then there is some **X** such that **ispossible(K) = True** if and only if **K >= X**.

For example, with **A = [3, 6, 7, 11]** and **H = 8**, there is some **X = 4** so that **ispossible(1) = ispossible(2) = ispossible(3) = False, and ispossible(4) = ispossible(5) = … = True**.

We can do a binary search on the values of **ispossible(K)** to find the first **X** such that **ispossible(X) is True** which will be our answer.

Now, as it is not allowed to move from one element to other during the current hour even if the job is completed than the maximum possible value of K can be the maximum element in the array A[]. So, to find the value of **ispossible(K)**, (i.e. whether person with a job speed of **K** can finish all jobs in **H** hours), do binary search in the range (1, max_element_of_array).

Also, for each **A[i]** of jobs > 0, we can deduce that person finishes it in **Math.ceil(A[i] / K)** or **((A[i]-1) / K) + 1** hours, and we add these for all elements to find the total time to complete all of the jobs and compare it with H to check if it is possible to finish all jobs with a speed of K jobs/hour.

Below is the implementation of above approach:

## C++

`// CPP program to find minimum speed ` `// to finish all jobs ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if the person can do ` `// all jobs in H hours with speed K ` `bool` `isPossible(` `int` `A[], ` `int` `n, ` `int` `H, ` `int` `K) ` `{ ` ` ` `int` `time` `= 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `time` `+= (A[i] - 1) / K + 1; ` ` ` ` ` `return` `time` `<= H; ` `} ` ` ` `// Function to return the minimum speed ` `// of person to complete all jobs ` `int` `minJobSpeed(` `int` `A[], ` `int` `n, ` `int` `H) ` `{ ` ` ` `// If H < N it is not possible to complete ` ` ` `// all jobs as person can not move from ` ` ` `// one element to another during current hour ` ` ` `if` `(H < n) ` ` ` `return` `-1; ` ` ` ` ` `// Max element of array ` ` ` `int` `* max = max_element(A, A + n); ` ` ` ` ` `int` `lo = 1, hi = *max; ` ` ` ` ` `// Use binary search to find smallest K ` ` ` `while` `(lo < hi) { ` ` ` `int` `mi = lo + (hi - lo) / 2; ` ` ` `if` `(!isPossible(A, n, H, mi)) ` ` ` `lo = mi + 1; ` ` ` `else` ` ` `hi = mi; ` ` ` `} ` ` ` ` ` `return` `lo; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `A[] = { 3, 6, 7, 11 }, H = 8; ` ` ` ` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` ` ` `// Print required maxLenwer ` ` ` `cout << minJobSpeed(A, n, H); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum speed ` `// to finish all jobs ` `class` `GFG ` `{ ` ` ` `// Function To findmax value in Array ` `static` `int` `findmax(` `int` `[] A) ` `{ ` ` ` `int` `r = A[` `0` `]; ` ` ` `for` `(` `int` `i = ` `1` `; i < A.length; i++) ` ` ` `r = Math.max(r, A[i]); ` ` ` `return` `r; ` `} ` ` ` `// Function to check if the person can do ` `// all jobs in H hours with speed K ` `static` `boolean` `isPossible(` `int` `[] A, ` `int` `n, ` ` ` `int` `H, ` `int` `K) ` `{ ` ` ` `int` `time = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `time += (A[i] - ` `1` `) / K + ` `1` `; ` ` ` ` ` `return` `time <= H; ` `} ` ` ` `// Function to return the minimum speed ` `// of person to complete all jobs ` `static` `int` `minJobSpeed(` `int` `[] A, ` ` ` `int` `n, ` `int` `H) ` `{ ` ` ` `// If H < N it is not possible to ` ` ` `// complete all jobs as person can ` ` ` `// not move from one element to ` ` ` `// another during current hour ` ` ` `if` `(H < n) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// Max element of array ` ` ` `int` `max = findmax(A); ` ` ` ` ` `int` `lo = ` `1` `, hi = max; ` ` ` ` ` `// Use binary search to find ` ` ` `// smallest K ` ` ` `while` `(lo < hi) ` ` ` `{ ` ` ` `int` `mi = lo + (hi - lo) / ` `2` `; ` ` ` `if` `(!isPossible(A, n, H, mi)) ` ` ` `lo = mi + ` `1` `; ` ` ` `else` ` ` `hi = mi; ` ` ` `} ` ` ` ` ` `return` `lo; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `[] A = { ` `3` `, ` `6` `, ` `7` `, ` `11` `}; ` ` ` `int` `H = ` `8` `; ` ` ` ` ` `int` `n = A.length; ` ` ` ` ` `// Print required maxLenwer ` ` ` `System.out.println(minJobSpeed(A, n, H)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum speed ` `// to finish all jobs ` ` ` `using` `System; ` `using` `System.Linq; ` ` ` `class` `GFG{ ` ` ` `// Function to check if the person can do ` `// all jobs in H hours with speed K ` `static` `bool` `isPossible(` `int` `[] A, ` `int` `n, ` `int` `H, ` `int` `K) ` `{ ` ` ` `int` `time = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `time += (A[i] - 1) / K + 1; ` ` ` ` ` `return` `time <= H; ` `} ` ` ` `// Function to return the minimum speed ` `// of person to complete all jobs ` `static` `int` `minJobSpeed(` `int` `[] A, ` `int` `n, ` `int` `H) ` `{ ` ` ` `// If H < N it is not possible to complete ` ` ` `// all jobs as person can not move from ` ` ` `// one element to another during current hour ` ` ` `if` `(H < n) ` ` ` `return` `-1; ` ` ` ` ` `// Max element of array ` ` ` `int` `max = A.Max(); ` ` ` ` ` `int` `lo = 1, hi = max; ` ` ` ` ` `// Use binary search to find smallest K ` ` ` `while` `(lo < hi) { ` ` ` `int` `mi = lo + (hi - lo) / 2; ` ` ` `if` `(!isPossible(A, n, H, mi)) ` ` ` `lo = mi + 1; ` ` ` `else` ` ` `hi = mi; ` ` ` `} ` ` ` ` ` `return` `lo; ` `} ` ` ` `// Driver program ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[] A = { 3, 6, 7, 11 }; ` ` ` `int` `H = 8; ` ` ` ` ` `int` `n = A.Length; ` ` ` ` ` `// Print required maxLenwer ` ` ` `Console.WriteLine(minJobSpeed(A, n, H)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find minimum ` `# speed to finish all jobs ` ` ` `# Function to check if the person can do ` `# all jobs in H hours with speed K ` `def` `isPossible(A, n, H, K): ` ` ` ` ` `time ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `time ` `+` `=` `(A[i] ` `-` `1` `) ` `/` `/` `K ` `+` `1` ` ` ` ` `return` `time <` `=` `H ` ` ` ` ` `# Function to return the minimum speed ` `# of person to complete all jobs ` `def` `minJobSpeed(A, n, H): ` ` ` ` ` `# If H < N it is not possible to complete ` ` ` `# all jobs as person can not move from ` ` ` `# one element to another during current hour ` ` ` `if` `H < n: ` ` ` `return` `-` `1` ` ` ` ` `# Max element of array ` ` ` `Max` `=` `max` `(A) ` ` ` ` ` `lo, hi ` `=` `1` `, ` `Max` ` ` ` ` `# Use binary search to find smallest K ` ` ` `while` `lo < hi: ` ` ` `mi ` `=` `lo ` `+` `(hi ` `-` `lo) ` `/` `/` `2` ` ` `if` `not` `isPossible(A, n, H, mi): ` ` ` `lo ` `=` `mi ` `+` `1` ` ` `else` `: ` ` ` `hi ` `=` `mi ` ` ` ` ` `return` `lo ` ` ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `A ` `=` `[` `3` `, ` `6` `, ` `7` `, ` `11` `] ` ` ` `H ` `=` `8` ` ` ` ` `n ` `=` `len` `(A) ` ` ` ` ` `# Print required maxLenwer ` ` ` `print` `(minJobSpeed(A, n, H)) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N*log(M)), where N is the length of array and M is max(A).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find minimum time to finish all jobs with given constraints
- Program to find the Speed of train as per speed of sound
- Program to find Length of Bridge using Speed and Length of Train
- Calculate speed, distance and time
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- Minimum number of operations to move all uppercase characters before all lower case characters
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Find minimum value to assign all array elements so that array product becomes greater
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Find Bit whose minimum sequence flips makes all bits same
- Find the minimum number of operations required to make all array elements equal
- Find the minimum value of the given expression over all pairs of the array
- Find minimum GCD of all pairs in an array
- Find minimum moves to bring all elements in one cell of a matrix
- Find all matrix elements which are minimum in their row and maximum in their column
- Find the element before which all the elements are smaller than it, and after which all are greater
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Maximum and minimum of an array using minimum number of comparisons
- Second minimum element using minimum comparisons

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.