# Minimum K such that sum of array elements after division by K does not exceed S

Given an array arr[] of N elements and an integer S. The task is to find the minimum number K such that the sum of the array elements does not exceed S after dividing all the elements by K.
Note: Consider integer division.

Examples:

Input: arr[] = {10, 7, 8, 10, 12, 19}, S = 27
Output: 3
After dividing by 3, the array becomes
{3, 2, 2, 3, 4, 6} and the new sum is 20.

Input: arr[] = {19, 17, 11, 10}, S = 40
Output: 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: Iterate for all values of K from 1 to the maximum element in the array and then sum up the array elements by dividing with K, if the sum does not exceed S then the current value will be the answer. The time complexity of this approach will be O(M * N) where M is the maximum element in the array.

Efficient approach: An efficient approach is to find the value of K by performing binary search on the answer. Initiate a binary search on the value of K and a check is done inside it to see if the sum exceeds K then the binary search is performed on the second half or the first half accordingly.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the minimum value of k ` `// that satisfies the given condition ` `int` `findMinimumK(``int` `a[], ``int` `n, ``int` `s) ` `{ ` `    ``// Find the maximum element ` `    ``int` `maximum = a[0]; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``maximum = max(maximum, a[i]); ` `    ``} ` ` `  `    ``// Lowest answer can be 1 and the ` `    ``// highest answer can be (maximum + 1) ` `    ``int` `low = 1, high = maximum + 1; ` ` `  `    ``int` `ans = high; ` ` `  `    ``// Binary search ` `    ``while` `(low <= high) { ` ` `  `        ``// Get the mid element ` `        ``int` `mid = (low + high) / 2; ` `        ``int` `sum = 0; ` ` `  `        ``// Calculate the sum after dividing ` `        ``// the array by new K which is mid ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``sum += (``int``)(a[i] / mid); ` `        ``} ` ` `  `        ``// Search in the second half ` `        ``if` `(sum > s) ` `            ``low = mid + 1; ` ` `  `        ``// First half ` `        ``else` `{ ` `            ``ans = min(ans, mid); ` `            ``high = mid - 1; ` `        ``} ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 10, 7, 8, 10, 12, 19 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``int` `s = 27; ` ` `  `    ``cout << findMinimumK(a, n, s); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GFG  ` `{ ` `     `  `    ``// Function to return the minimum value of k  ` `    ``// that satisfies the given condition  ` `    ``static` `int` `findMinimumK(``int` `a[],  ` `                            ``int` `n, ``int` `s)  ` `    ``{  ` `        ``// Find the maximum element  ` `        ``int` `maximum = a[``0``];  ` `         `  `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{  ` `            ``maximum = Math.max(maximum, a[i]);  ` `        ``}  ` `     `  `        ``// Lowest answer can be 1 and the  ` `        ``// highest answer can be (maximum + 1)  ` `        ``int` `low = ``1``, high = maximum + ``1``;  ` `     `  `        ``int` `ans = high;  ` `     `  `        ``// Binary search  ` `        ``while` `(low <= high) ` `        ``{  ` `     `  `            ``// Get the mid element  ` `            ``int` `mid = (low + high) / ``2``;  ` `            ``int` `sum = ``0``;  ` `     `  `            ``// Calculate the sum after dividing  ` `            ``// the array by new K which is mid  ` `            ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``{  ` `                ``sum += (``int``)(a[i] / mid);  ` `            ``}  ` `     `  `            ``// Search in the second half  ` `            ``if` `(sum > s)  ` `                ``low = mid + ``1``;  ` `     `  `            ``// First half  ` `            ``else` `            ``{  ` `                ``ans = Math.min(ans, mid);  ` `                ``high = mid - ``1``;  ` `            ``}  ` `        ``}  ` `        ``return` `ans;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `a[] = { ``10``, ``7``, ``8``, ``10``, ``12``, ``19` `};  ` `        ``int` `n = a.length;  ` `        ``int` `s = ``27``;  ` `     `  `        ``System.out.println(findMinimumK(a, n, s));  ` `    ``}  ` `}     ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the minimum value of k ` `# that satisfies the given condition ` `def` `findMinimumK(a, n, s): ` `     `  `    ``# Find the maximum element ` `    ``maximum ``=` `a[``0``] ` `    ``for` `i ``in` `range``(n): ` `        ``maximum ``=` `max``(maximum, a[i]) ` ` `  `    ``# Lowest answer can be 1 and the ` `    ``# highest answer can be (maximum + 1) ` `    ``low ``=` `1` `    ``high ``=` `maximum ``+` `1` ` `  `    ``ans ``=` `high ` ` `  `    ``# Binary search ` `    ``while` `(low <``=` `high): ` ` `  `        ``# Get the mid element ` `        ``mid ``=` `(low ``+` `high) ``/``/` `2` `        ``sum` `=` `0` ` `  `        ``# Calculate the sum after dividing ` `        ``# the array by new K which is mid ` `        ``for` `i ``in` `range``(n): ` `            ``sum` `+``=` `(a[i] ``/``/` `mid) ` ` `  `        ``# Search in the second half ` `        ``if` `(``sum` `> s): ` `            ``low ``=` `mid ``+` `1` ` `  `        ``# First half ` `        ``else``: ` `            ``ans ``=` `min``(ans, mid) ` `            ``high ``=` `mid ``-` `1` ` `  `    ``return` `ans ` ` `  `# Driver code ` `a ``=` `[``10``, ``7``, ``8``, ``10``, ``12``, ``19``] ` `n ``=` `len``(a) ` `s ``=` `27` ` `  `print``(findMinimumK(a, n, s)) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `     `  `    ``// Function to return the minimum value of k  ` `    ``// that satisfies the given condition  ` `    ``static` `int` `findMinimumK(``int` `[]a,  ` `                            ``int` `n, ``int` `s)  ` `    ``{  ` `        ``// Find the maximum element  ` `        ``int` `maximum = a[0];  ` `         `  `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{  ` `            ``maximum = Math.Max(maximum, a[i]);  ` `        ``}  ` `     `  `        ``// Lowest answer can be 1 and the  ` `        ``// highest answer can be (maximum + 1)  ` `        ``int` `low = 1, high = maximum + 1;  ` `     `  `        ``int` `ans = high;  ` `     `  `        ``// Binary search  ` `        ``while` `(low <= high)  ` `        ``{  ` `     `  `            ``// Get the mid element  ` `            ``int` `mid = (low + high) / 2;  ` `            ``int` `sum = 0;  ` `     `  `            ``// Calculate the sum after dividing  ` `            ``// the array by new K which is mid  ` `            ``for` `(``int` `i = 0; i < n; i++)  ` `            ``{  ` `                ``sum += (``int``)(a[i] / mid);  ` `            ``}  ` `     `  `            ``// Search in the second half  ` `            ``if` `(sum > s)  ` `                ``low = mid + 1;  ` `     `  `            ``// First half  ` `            ``else` `            ``{  ` `                ``ans = Math.Min(ans, mid);  ` `                ``high = mid - 1;  ` `            ``}  ` `        ``}  ` `        ``return` `ans;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main ()  ` `    ``{  ` `        ``int` `[]a = { 10, 7, 8, 10, 12, 19 };  ` `        ``int` `n = a.Length;  ` `        ``int` `s = 27;  ` `     `  `        ``Console.WriteLine(findMinimumK(a, n, s));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by AnkitRai01  `

Output:

```3
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : mohit kumar 29, AnkitRai01