You are given an array of integers 'arr' and an integer 'limit'.
Your task is to find the smallest positive integer divisor, such that upon dividing all the elements of the given array by it, the sum of the division's result is less than or equal to the given integer's limit.
Each result of the division is rounded to the nearest integer greater than or equal to that element. For Example, 7/3 = 3.
The first line contains an integer ‘n’ denoting the number of elements in the array.
The second line contains ‘n’ Space-separated integers denoting the elements of the array.
The third line contains an integer ‘limit’ denoting the given 'limit'.
Output format :
Print an integer denoting the minimum divisor.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
5
1 2 3 4 5
8
3
We can get a sum of 15(1 + 2 + 3 + 4 + 5) if we choose 1 as a divisor.
The sum is 9(1 + 1 + 2 + 2 + 3) if we choose 2 as a divisor, and the sum is 7(1 + 1 + 1 + 2 + 2) if we choose 3 as a divisor, which is less than the 'limit'.
Hence we return 3.
4
8 4 2 3
10
2
We can get a sum of 17(8 + 4 + 2 + 3) if we choose 1 as a divisor.
The sum is 9(4 + 2 + 1 + 2) if we choose 2 as a divisor, which is less than the 'limit'.
Hence, we return 2.
5
2 3 5 7 11
11
3
1 <= n <= 10 ^ 5
1 <= arr[i] <= 10 ^ 6
N <= limit <= 10 ^ 4
Time Limit: 1 sec.
Try to find every possible solution and choose the largest of them.
The approach is to find the minimum divisor from 1 to the maximum element of the input array. We keep on selecting the divisor until we get the result.
O(N * M), where ‘N’ is the length of the given array, and ‘M’ is the value of the maximum element from the given array.
As we are making nested iterations for each element of the array by dividing every element of the array with the integers from 1 to the maximum element of the array. Therefore, the overall time complexity will be O(N * M).
O(1)
As we are using constant space for the approach. Therefore, the overall space complexity will be O(1).