Minimum capacity of small arrays needed to contain all element of the given array
Given an array of positive integers and a value K, The task is to empty the array in less than or equal to K small arrays such that each small array can only contain at max P elements from a single slot / index of the given array. Find the minimum value of P.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}, K = 7
Output: 3
Explanation:
We put 1 into the first small array,
2 into the second array,
3 into the third array,
After this, we divide the other elements as 1 + 3 and 2 + 3
These 4 elements can be put into the remaining 4 boxes.
So, the required value of P is 3.
Input: arr[] = {23, 1, 43, 66, 220}, K = 102
Output: 4
Approach: To solve this problem we need to Binary Search the answer.
- First, we set the lower limit to 1 and the upper limit to the maximum value of the given array.
- Now, we can perform a binary search in this range. For a particular value of capacity, we calculate the number of small arrays we need to contain all values from items.
- If this required number of small arrays is more than K, the answer is definitely bigger hence, we trim the left side of search. If it is less than or equal to K, we keep this value as a potential answer and trim the right side of the search.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int MinimumCapacity(vector< int > arr,
int K)
{
int maxVal = arr[0];
for ( auto x : arr)
maxVal = max(maxVal, x);
int l = 1, r = maxVal, m;
int ans, req;
while (l <= r)
{
m = l + (r - l) / 2;
req = 0;
for ( auto x : arr)
req += x / m + (x % m > 0);
if (req > K)
l = m + 1;
else
ans = m, r = m - 1;
}
return ans;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4, 5 };
int K = 7;
cout << MinimumCapacity(arr, K);
return 0;
}
|
Java
class GFG{
static int MinimumCapacity( int []arr,
int K)
{
int maxVal = arr[ 0 ];
for ( int x : arr)
maxVal = Math.max(maxVal, x);
int l = 1 , r = maxVal, m;
int ans = 0 , req;
while (l <= r)
{
m = l + (r - l) / 2 ;
req = 0 ;
for ( int x : arr)
req += x / m + (x % m > 0 ? 1 : 0 );
if (req > K)
l = m + 1 ;
else
{
ans = m;
r = m - 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int []arr = { 1 , 2 , 3 , 4 , 5 };
int K = 7 ;
System.out.print(MinimumCapacity(arr, K));
}
}
|
Python3
def MinimumCapacity(arr, K):
maxVal = arr[ 0 ]
for x in arr:
maxVal = max (maxVal, x)
l = 1
r = maxVal
m = 0
ans = 0
req = 0
while l < = r:
m = l + (r - l) / / 2
req = 0
for x in arr:
req + = x / / m + (x % m > 0 )
if req > K:
l = m + 1
else :
ans = m
r = m - 1
return ans
arr = [ 1 , 2 , 3 , 4 , 5 ]
K = 7
print (MinimumCapacity(arr, K))
|
C#
using System;
class GFG{
static int MinimumCapacity( int []arr,
int K)
{
int maxVal = arr[0];
foreach ( int x in arr)
maxVal = Math.Max(maxVal, x);
int l = 1, r = maxVal, m;
int ans = 0, req;
while (l <= r)
{
m = l + (r - l) / 2;
req = 0;
foreach ( int x in arr)
req += x / m + (x % m > 0 ? 1 : 0);
if (req > K)
l = m + 1;
else
{
ans = m;
r = m - 1;
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int K = 7;
Console.Write(MinimumCapacity(arr, K));
}
}
|
Javascript
<script>
function MinimumCapacity(arr, K)
{
let maxVal = arr[0];
for (let x = 0; x < arr.length; x++)
maxVal = Math.max(maxVal, arr[x]);
let l = 1, r = maxVal, m;
let ans = 0, req;
while (l <= r)
{
m = l + parseInt((r - l) / 2, 10);
req = 0;
for (let x = 0; x < arr.length; x++)
req +=
parseInt(arr[x] / m, 10) + (arr[x] % m > 0 ? 1 : 0);
if (req > K)
l = m + 1;
else
{
ans = m;
r = m - 1;
}
}
return ans;
}
let arr = [ 1, 2, 3, 4, 5 ];
let K = 7;
document.write(MinimumCapacity(arr, K));
</script>
|
Last Updated :
15 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...