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 = 6Output:4Explanation:

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 = 4Output:8

**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.

- Input the array.
- On assuming that the maximum possible answer is 10
^{9}, initialize the max as 10^{9}and the min as 1. - Perform the Binary Search on this range and for every number, check if the sum is less than or equal to K.
- 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.
- 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++ 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 <bits/stdc++.h> ` `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); `
`} ` |

*chevron_right*

*filter_none*

`// 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 ` |

*chevron_right*

*filter_none*

`# 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 ` |

*chevron_right*

*filter_none*

`// 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 ` |

*chevron_right*

*filter_none*

**Output:**

4

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

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:

- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Form an array of distinct elements with each element as sum of an element from each array
- Sum of product of each element with each element after it
- Largest number dividing maximum number of elements in the array
- Dividing an array into two halves of same sum
- Array sum after dividing numbers from previous
- Maximum sum of the array after dividing it into three segments
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, N]
- Reduce the array such that each element appears at most 2 times
- Reduce the array such that each element appears at most K times
- Count number of digits after decimal on dividing a number
- Minimum pair sum operations to make array each element divisible by 4
- Find Array formed by adding each element of given array with largest element in new array to its left
- Maximum sum after repeatedly dividing N by a divisor
- Minimize sum by dividing all elements of a subarray by K
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Min steps to empty an Array by removing a pair each time with sum at most K
- Smallest subarray with all occurrences of a most frequent element

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.