Given an array arr[] of size N and a positive integer K, the task is to find the smallest positive integer such that the sum of remaining array elements obtained by dividing all array elements by that smallest positive integer does not exceed K.
Note: Dividing an array element by the smallest positive integer must be of Ceil type.
Examples:
Input: arr[] = {1, 2, 5, 9}, K = 6
Output: 5
Explanation:
Dividing all array elements by 5 modifies arr[] to {1, 1, 1, 2}.
Since the sum of the array elements is equal to 5 ( < K), the required output is 5.
Input: arr[]= {2, 3, 5, 7, 11}, K = 11
Output: 3
Naive Approach:The simplest approach to solve this problem is to find the largest element in the array, say Max, and iterate over the range [1, Max] using the variable i and check if the sum of the remaining array elements after dividing all the array elements by i is less than or equal to K or not. If found to be true, then print i.
Time Complexity: O(N * Max), where Max is the largest element of the array.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is to use Binary search technique. Follow the steps below to solve the problem:
- Initialize a variable, say Max, to store the largest element present in the array.
- The value of the smallest positive integer which divides all the array elements to get the sum of remaining array elements less than or equal to K, must lie in the range [1, Max]. Therefore, apply binary search over the range [1, Max].
- Initialize two variables, say left = 1 and right = Max, to store the range in which the value of the required output lies.
- Check if it is possible to get the sum of the array elements less than or equal to K by dividing the array elements by (left + right) / 2 or not. If found to be true, then update right = (left + right) / 2.
- Otherwise, update left = (left + right) /2.
- Finally, print the value of left.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSmallestInteger( int arr[], int N, int K)
{
int left = 1;
int right = *max_element(arr, arr + N);
while (left < right) {
int mid = (left + right) / 2;
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += (arr[i] + mid - 1) / mid;
}
if (sum > K) {
left = mid + 1;
}
else {
right = mid;
}
}
return left;
}
int main()
{
int arr[] = { 1, 2, 5, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
;
int K = 6;
cout << findSmallestInteger(arr, N, K);
}
|
Java
import java.util.*;
class GFG{
static int findSmallestInteger( int arr[],
int N, int K)
{
int left = 1 ;
int right = Arrays.stream(arr).max().getAsInt();
while (left < right)
{
int mid = (left + right) / 2 ;
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += (arr[i] + mid - 1 ) / mid;
}
if (sum > K)
{
left = mid + 1 ;
}
else
{
right = mid;
}
}
return left;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 5 , 9 };
int N = arr.length;
int K = 6 ;
System.out.println(findSmallestInteger(arr, N, K));
}
}
|
Python3
def findSmallestInteger(arr, N, K):
left = 1
right = max (arr)
while (left < right):
mid = (left + right) / / 2
sum = 0
for i in range (N):
sum + = (arr[i] + mid - 1 ) / / mid
if ( sum > K):
left = mid + 1
else :
right = mid
return left
if __name__ = = '__main__' :
arr = [ 1 , 2 , 5 , 9 ]
N = len (arr)
K = 6
print (findSmallestInteger(arr, N, K))
|
C#
using System;
using System.Linq;
class GFG{
static int findSmallestInteger( int [] arr,
int N, int K)
{
int left = 1;
int right = arr.Max();
while (left < right)
{
int mid = (left + right) / 2;
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += (arr[i] + mid - 1) / mid;
}
if (sum > K)
{
left = mid + 1;
}
else
{
right = mid;
}
}
return left;
}
public static void Main()
{
int [] arr = { 1, 2, 5, 9 };
int N = arr.Length;
int K = 6;
Console.Write(findSmallestInteger(arr, N, K));
}
}
|
Javascript
<script>
function findSmallestInteger(arr, N, K)
{
var left = 1;
var right = arr.reduce((a, b) => Math.max(a, b));
while (left < right)
{
var mid = (left + right) / 2;
var sum = 0;
for ( var i = 0; i < N; i++)
{
sum += parseInt((arr[i] + mid - 1) / mid);
}
if (sum > K)
{
left = mid + 1;
}
else
{
right = mid;
}
}
return left;
}
var arr = [ 1, 2, 5, 9 ];
var N = arr.length;
var K = 6;
document.write(findSmallestInteger(arr, N, K));
</script>
|
Time Complexity: O(N * log(Max)), where Max is the largest element of the array.
Auxiliary Space: O(1)