# Smallest number to make Array sum at most K by dividing each element

Given an array arr[] of size N and a number K, the task is to find the smallest number M such that the sum of the array becomes lesser than or equal to the number K when every element of that array is divided by the number M.

Note: Each result of the division is rounded to the nearest integer greater than or equal to that element. For example: 10/3 = 4 and 6/2 = 3

Examples:

Input: arr[] = {2, 3, 4, 9}, K = 6
Output: 4
Explanation:
When every element is divided by 4- 2/4 + 3/4 + 4/4 + 9/4 = 1 + 1 + 1 + 3 = 6
When every element is divided by 3- 2/3 + 3/3 + 4/3 + 9/3 = 1 + 1 + 2 + 3 = 7 which is greater than K.
Hence the smallest integer which makes the sum less than or equal to K = 6 is 4.

Input: arr[] = {5, 6, 7, 8}, K = 4
Output: 8

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

Naive Approach: The naive approach for this problem is to start from 1 and for every number, divide every element in the array and check if the sum is less than or equal to K. The first number at which this condition satisfies is the required answer.

Time complexity: O(N * M), where M is the number to be found and N is the size of the array.

Efficient Approach: The idea is to use the concept of Binary Search.

1. Input the array.
2. On assuming that the maximum possible answer is 109, initialize the max as 109 and the min as 1.
3. Perform the Binary Search on this range and for every number, check if the sum is less than or equal to K.
4. If the sum is less than K, then an answer might exist for a number which is smaller than this. So, continue and check for the numbers less than that counter.
5. If the sum is greater than K, then the number M is greater than the current counter. So, continue and check for the numbers greater than that counter.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the smallest ` `// number such that the sum of the ` `// array becomes less than or equal ` `// to K when every element of the ` `// array is divided by that number ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the smallest ` `// number such that the sum of the ` `// array becomes less than or equal ` `// to K when every element of the ` `// array is divided by that number ` `int` `findMinDivisor(``int` `arr[], ``int` `n, ``int` `limit) ` `{ ` `    ``// Binary search between 1 and 10^9 ` `    ``int` `low = 0, high = 1e9; ` `    ``while` `(low < high) { ` `        ``int` `mid = (low + high) / 2; ` `        ``int` `sum = 0; ` ` `  `        ``// Calculating the new sum after ` `        ``// dividing every element by mid ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``sum += ``ceil``((``double``)arr[i] ` `                        ``/ (``double``)mid); ` `        ``} ` ` `  `        ``// If after dividing by mid, ` `        ``// if the new sum is less than or ` `        ``// equal to limit move low to mid+1 ` `        ``if` `(sum <= limit) ` `            ``high = mid; ` `        ``else` ` `  `            ``// Else, move mid + 1 to high ` `            ``low = mid + 1; ` `    ``} ` ` `  `    ``// Returning the minimum number ` `    ``return` `low; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 3, 4, 9 }; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``int` `K = 6; ` ` `  `    ``cout << findMinDivisor(arr, N, K); ` `} `

## Java

 `// Java program to find the smallest  ` `// number such that the sum of the  ` `// array becomes less than or equal  ` `// to K when every element of the  ` `// array is divided by that number  ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to find the smallest  ` `// number such that the sum of the  ` `// array becomes less than or equal  ` `// to K when every element of the  ` `// array is divided by that number  ` `static` `int` `findMinDivisor(``int` `arr[],  ` `                          ``int` `n, ``int` `limit) ` `{ ` `     `  `    ``// Binary search between 1 and 10^9  ` `    ``int` `low = ``0``, high = ``1000000000``; ` `     `  `    ``while` `(low < high) ` `    ``{ ` `        ``int` `mid = (low + high) / ``2``; ` `        ``int` `sum = ``0``; ` `     `  `        ``// Calculating the new sum after  ` `        ``// dividing every element by mid  ` `        ``for``(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `           ``sum += Math.ceil((``double``) arr[i] /  ` `                            ``(``double``) mid); ` `        ``} ` `     `  `        ``// If after dividing by mid,  ` `        ``// if the new sum is less than or  ` `        ``// equal to limit move low to mid+1  ` `        ``if` `(sum <= limit) ` `            ``high = mid; ` `        ``else` `         `  `            ``// Else, move mid + 1 to high  ` `            ``low = mid + ``1``; ` `    ``} ` ` `  `    ``// Returning the minimum number  ` `    ``return` `low; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``2``, ``3``, ``4``, ``9` `}; ` `    ``int` `N = arr.length; ` `    ``int` `K = ``6``; ` ` `  `    ``System.out.println( ` `           ``findMinDivisor(arr, N, K)); ` `} ` `} ` ` `  `// This code is contributed by rutvik_56 `

## Python3

 `# Python3 program to find the smallest ` `# number such that the sum of the ` `# array becomes less than or equal ` `# to K when every element of the ` `# array is divided by that number ` `from` `math ``import` `ceil ` ` `  `# Function to find the smallest ` `# number such that the sum of the ` `# array becomes less than or equal ` `# to K when every element of the ` `# array is divided by that number ` `def` `findMinDivisor(arr, n, limit): ` `     `  `    ``# Binary search between 1 and 10^9 ` `    ``low ``=` `0` `    ``high ``=` `10` `*``*` `9` `     `  `    ``while` `(low < high): ` `        ``mid ``=` `(low ``+` `high) ``/``/` `2` `        ``sum` `=` `0` ` `  `        ``# Calculating the new sum after ` `        ``# dividing every element by mid ` `        ``for` `i ``in` `range``(n): ` `            ``sum` `+``=` `ceil(arr[i] ``/` `mid) ` ` `  `        ``# If after dividing by mid, ` `        ``# if the new sum is less than or ` `        ``# equal to limit move low to mid+1 ` `        ``if` `(``sum` `<``=` `limit): ` `            ``high ``=` `mid ` `        ``else``: ` ` `  `            ``# Else, move mid + 1 to high ` `            ``low ``=` `mid ``+` `1` ` `  `    ``# Returning the minimum number ` `    ``return` `low ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``arr``=` `[ ``2``, ``3``, ``4``, ``9` `] ` `    ``N ``=` `len``(arr) ` `    ``K ``=` `6` `     `  `    ``print``(findMinDivisor(arr, N, K)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# program to find the smallest  ` `// number such that the sum of the  ` `// array becomes less than or equal  ` `// to K when every element of the  ` `// array is divided by that number  ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to find the smallest  ` `// number such that the sum of the  ` `// array becomes less than or equal  ` `// to K when every element of the  ` `// array is divided by that number  ` `static` `int` `findMinDivisor(``int` `[]arr, ``int` `n, ` `                          ``int` `limit) ` `{ ` `     `  `    ``// Binary search between 1 and 10^9  ` `    ``int` `low = 0, high = 1000000000; ` `     `  `    ``while` `(low < high) ` `    ``{ ` `        ``int` `mid = (low + high) / 2; ` `        ``int` `sum = 0; ` `     `  `        ``// Calculating the new sum after  ` `        ``// dividing every element by mid  ` `        ``for``(``int` `i = 0; i < n; i++) ` `        ``{ ` `           ``sum += (``int``)Math.Ceiling((``double``) arr[i] /  ` `                                    ``(``double``) mid); ` `        ``} ` `         `  `        ``// If after dividing by mid,  ` `        ``// if the new sum is less than or  ` `        ``// equal to limit move low to mid+1  ` `        ``if` `(sum <= limit) ` `        ``{ ` `            ``high = mid; ` `        ``} ` `        ``else` `        ``{ ` ` `  `            ``// Else, move mid + 1 to high  ` `            ``low = mid + 1; ` `        ``} ` `    ``} ` `     `  `    ``// Returning the minimum number  ` `    ``return` `low; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `[]arr = { 2, 3, 4, 9 }; ` `    ``int` `N = arr.Length; ` `    ``int` `K = 6; ` ` `  `    ``Console.WriteLine(findMinDivisor(arr, N, K)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```4
```

Time Complexity: O(N * 30), where N is the size of the array because finding any number between 1 and 109 takes atmost 30 operations in binary search.

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.