Smallest number to make Array sum at most K by dividing each element
Last Updated :
14 Sep, 2022
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
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 109, initialize the max as 109 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 that 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++
#include <bits/stdc++.h>
using namespace std;
int findMinDivisor( int arr[], int n, int limit)
{
int low = 0, high = 1e9;
while (low < high) {
int mid = (low + high) / 2;
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += ceil (( double )arr[i]
/ ( double )mid);
}
if (sum <= limit)
high = mid;
else
low = mid + 1;
}
return low;
}
int main()
{
int arr[] = { 2, 3, 4, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 6;
cout << findMinDivisor(arr, N, K);
}
|
Java
import java.util.*;
class GFG{
static int findMinDivisor( int arr[],
int n, int limit)
{
int low = 0 , high = 1000000000 ;
while (low < high)
{
int mid = (low + high) / 2 ;
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum += Math.ceil(( double ) arr[i] /
( double ) mid);
}
if (sum <= limit)
high = mid;
else
low = mid + 1 ;
}
return low;
}
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));
}
}
|
Python3
from math import ceil
def findMinDivisor(arr, n, limit):
low = 0
high = 10 * * 9
while (low < high):
mid = (low + high) / / 2
sum = 0
for i in range (n):
sum + = ceil(arr[i] / mid)
if ( sum < = limit):
high = mid
else :
low = mid + 1
return low
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 9 ]
N = len (arr)
K = 6
print (findMinDivisor(arr, N, K))
|
C#
using System;
class GFG{
static int findMinDivisor( int []arr, int n,
int limit)
{
int low = 0, high = 1000000000;
while (low < high)
{
int mid = (low + high) / 2;
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum += ( int )Math.Ceiling(( double ) arr[i] /
( double ) mid);
}
if (sum <= limit)
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
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));
}
}
|
Javascript
<script>
function findMinDivisor(arr, n, limit)
{
let low = 0, high = 1000000000;
while (low < high)
{
let mid = Math.floor((low + high) / 2);
let sum = 0;
for (let i = 0; i < n; i++)
{
sum += Math.ceil( arr[i] / mid);
}
if (sum <= limit)
high = mid;
else
low = mid + 1;
}
return low;
}
let arr = [ 2, 3, 4, 9 ];
let N = arr.length;
let K = 6;
document.write(
findMinDivisor(arr, N, K));
</script>
|
Time Complexity: O(N * 30), where N is the size of the array because finding any number between 1 and 109 takes at most 30 operations in binary search.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...