Smallest possible integer K such that ceil of each Array element when divided by K is at most M
Last Updated :
01 Oct, 2021
Given an array arr[] consisting of N positive integers and a positive integer M, the task is to find the smallest possible integer K such that ceil(arr[0]/K) + ceil(arr[1]/K) +….+ ceil(arr[N – 1]/K) is at most M.
Examples:
Input: arr[] = {4, 3, 2, 7}, M = 5
Output: 4
Explanation:
For K = 4, the value of ceil(4/4) + ceil(3/4) + ceil(2/4) + ceil(7/4) = 1 + 1 + 1 + 2 = 5. Therefore, print 5.
Input: arr[] = {1, 2, 3}, M = 4
Output: 2
Approach: The idea is to use the Binary Search. Set the low value as 1 and high value as a maximum value from the array arr[] and find the K value which is less than or equal to M by applying binary search. Follow the steps below to solve the problem:
- Initialize variables, say low = 1 and high as the maximum array element.
- Iterate over a while loop till high – low > 1 and perform the following tasks:
- Update the value of mid by mid = (low + high)/2.
- Traverse the array arr[] and find the sum of ceil(arr[i]/K) by assuming mid as K.
- If the sum is greater than M, then update the value of high to high = mid. Otherwise, update the value of low to low = mid + 1.
- After completing the above steps, print the value of low if the sum is at most M. Otherwise, print the value of high.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isvalid( int arr[], int K, int N, int M)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += ( int ) ceil (arr[i] * 1.0 / K);
}
return sum <= M;
}
int smallestValueForK( int arr[], int N, int M)
{
int low = 1;
int high = *max_element(arr, arr + N);
int mid;
while (high - low > 1) {
mid = (high + low) / 2;
if (!isvalid(arr, mid, N, M))
low = mid + 1;
else
high = mid;
}
return isvalid(arr, low, N, M) ? low : high;
}
int main()
{
int arr[] = { 4, 3, 2, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = 5;
cout << smallestValueForK(arr, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean isvalid( int [] arr, int K, int N, int M)
{
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
sum += Math.ceil(arr[i] * 1.0 / K);
}
return sum <= M;
}
static int smallestValueForK( int [] arr, int N, int M)
{
int low = 1 ;
int high = arr[ 0 ];
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] > high)
high = arr[i];
}
int mid;
while (high - low > 1 ) {
mid = (high + low) / 2 ;
if (isvalid(arr, mid, N, M)== false )
low = mid + 1 ;
else
high = mid;
}
return isvalid(arr, low, N, M) ? low : high;
}
public static void main(String args[])
{
int arr[] = { 4 , 3 , 2 , 7 };
int N = arr.length;
int M = 5 ;
System.out.print(smallestValueForK(arr, N, M));
}
}
|
Python3
import math
def isvalid(arr, K, N, M):
sum = 0
for i in range ( 0 , N):
sum + = math.ceil(arr[i] * 1.0 / K)
return sum < = M
def smallestValueForK(arr, N, M):
low = 1
high = arr[ 0 ]
for i in range ( 1 , len (arr)):
high = max (high, arr[i])
mid = 0
while (high - low > 1 ):
mid = (high + low) / / 2
if ( not isvalid(arr, mid, N, M)):
low = mid + 1
else :
high = mid
if (isvalid(arr, low, N, M)):
return low
else :
return high
if __name__ = = "__main__" :
arr = [ 4 , 3 , 2 , 7 ]
N = len (arr)
M = 5
print (smallestValueForK(arr, N, M))
|
C#
using System;
using System.Linq;
class GFG {
static bool isvalid( int [] arr, int K, int N, int M)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += ( int )Math.Ceiling(arr[i] * 1.0 / K);
}
return sum <= M;
}
static int smallestValueForK( int [] arr, int N, int M)
{
int low = 1;
int high = arr.Max();
int mid;
while (high - low > 1) {
mid = (high + low) / 2;
if (!isvalid(arr, mid, N, M))
low = mid + 1;
else
high = mid;
}
return isvalid(arr, low, N, M) ? low : high;
}
public static void Main()
{
int [] arr = { 4, 3, 2, 7 };
int N = arr.Length;
int M = 5;
Console.WriteLine(smallestValueForK(arr, N, M));
}
}
|
Javascript
<script>
function isvalid(arr, K, N, M)
{
let sum = 0;
for (let i = 0; i < N; i++) {
sum += Math.ceil(arr[i] * 1.0 / K);
}
return sum <= M;
}
function smallestValueForK(arr, N, M) {
let low = 1;
let high = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
high = Math.max(high, arr[i]);
}
let mid;
while (high - low > 1) {
mid = (high + low) / 2;
if (!isvalid(arr, mid, N, M))
low = mid + 1;
else
high = mid;
}
return isvalid(arr, low, N, M) ? low : high;
}
let arr = [4, 3, 2, 7];
let N = arr.length;
let M = 5;
document.write(smallestValueForK(arr, N, M));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...