Skip to content
Related Articles

Related Articles

Improve Article

Minimum capacity of small arrays needed to contain all element of the given array

  • Last Updated : 15 Jun, 2021
Geek Week

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:
 

 

Approach: To solve this problem we need to Binary Search the answer. 
 

  1. First, we set the lower limit to 1 and the upper limit to the maximum value of the given array.
  2. 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.
  3. 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++




// C++ program to find the Minimum
// capacity of small arrays needed
// to contain all element of
// the given array
#include <bits/stdc++.h>
using namespace std;
 
 
// Function returns the value
// of Minimum capacity needed
int MinimumCapacity(vector<int> arr,
                    int K)
{
    // Initializing maximum
    // value
    int maxVal = arr[0];
 
    // Finding maximum value
    // in arr
    for (auto x : arr)
        maxVal = max(maxVal, x);
 
    int l = 1, r = maxVal, m;
    int ans, req;
 
    // Binary Search the answer
    while (l <= r)
    {
 
        // m is the mid-point
        // of the range
        m = l + (r - l) / 2;
 
        req = 0;
 
        // Finding the total number of
        // arrays needed to completely
        // contain the items array
        // with P = req
        for (auto x : arr)
            req += x / m + (x % m > 0);
         
        // If the required number of
        // arrays is more than K, it
        // means we need to increase
        // the value of P
        if (req > K)
            l = m + 1;
 
        else
            // If the required number of
            // arrays is less than or equal
            // to K, it means this is a
            // possible answer and we go to
            // check if any smaller possible
            // value exists for P
            ans = m, r = m - 1;
    }
 
    return ans;
}
     
// Driver Code
int main()
{
 
    // Given array
    vector<int> arr = { 1, 2, 3, 4, 5 };
 
    // Number of available small arrays
    int K = 7;
 
    cout << MinimumCapacity(arr, K);
 
    return 0;
}

Java




// Java program to find the Minimum
// capacity of small arrays needed
// to contain all element of
// the given array
class GFG{
 
// Function returns the value
// of Minimum capacity needed
static int MinimumCapacity(int []arr,
                           int K)
{
    // Initializing maximum
    // value
    int maxVal = arr[0];
 
    // Finding maximum value
    // in arr
    for (int x : arr)
        maxVal = Math.max(maxVal, x);
 
    int l = 1, r = maxVal, m;
    int ans = 0, req;
 
    // Binary Search the answer
    while (l <= r)
    {
 
        // m is the mid-point
        // of the range
        m = l + (r - l) / 2;
 
        req = 0;
 
        // Finding the total number of
        // arrays needed to completely
        // contain the items array
        // with P = req
        for (int x : arr)
            req += x / m + (x % m > 0 ? 1 : 0);
         
        // If the required number of
        // arrays is more than K, it
        // means we need to increase
        // the value of P
        if (req > K)
            l = m + 1;
 
        else
        {
            // If the required number of
            // arrays is less than or equal
            // to K, it means this is a
            // possible answer and we go to
            // check if any smaller possible
            // value exists for P
            ans = m;
            r = m - 1;
        }
    }
 
    return ans;
}
     
// Driver Code
public static void main(String[] args)
{
 
    // Given array
    int []arr = { 1, 2, 3, 4, 5 };
 
    // Number of available small arrays
    int K = 7;
 
    System.out.print(MinimumCapacity(arr, K));
}
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 program to find the Minimum
# capacity of small arrays needed
# to contain all element of
# the given array
 
# Function returns the value
# of Minimum capacity needed
def MinimumCapacity(arr, K):
 
    # Initializing maximum
    # value
    maxVal = arr[0]
 
    # Finding maximum value
    # in arr
    for x in arr:
        maxVal = max(maxVal, x)
 
    l = 1
    r = maxVal
    m = 0
    ans = 0
    req = 0
 
    # Binary Search the answer
    while l <= r:
 
        # m is the mid-point
        # of the range
        m = l + (r - l) // 2
 
        req = 0
 
        # Finding the total number of
        # arrays needed to completely
        # contain the items array
        # with P = req
        for x in arr:
            req += x // m + (x % m > 0)
         
        # If the required number of
        # arrays is more than K, it
        # means we need to increase
        # the value of P
        if req > K:
            l = m + 1
 
        else:
             
            # If the required number of
            # arrays is less than or equal
            # to K, it means this is a
            # possible answer and we go to
            # check if any smaller possible
            # value exists for P
            ans = m
            r = m - 1
 
    return ans
     
#Driver Code
# Given array
arr = [ 1, 2, 3, 4, 5 ]
 
# Number of available small arrays
K = 7
print(MinimumCapacity(arr, K))
 
# This code is contributed by divyamohan123

C#




// C# program to find the minimum
// capacity of small arrays needed
// to contain all element of
// the given array
using System;
 
class GFG{
 
// Function returns the value
// of minimum capacity needed
static int MinimumCapacity(int []arr,
                           int K)
{
     
    // Initializing maximum
    // value
    int maxVal = arr[0];
 
    // Finding maximum value
    // in arr
    foreach (int x in arr)
        maxVal = Math.Max(maxVal, x);
 
    int l = 1, r = maxVal, m;
    int ans = 0, req;
 
    // Binary Search the answer
    while (l <= r)
    {
 
        // m is the mid-point
        // of the range
        m = l + (r - l) / 2;
 
        req = 0;
         
        // Finding the total number of
        // arrays needed to completely
        // contain the items array
        // with P = req
        foreach (int x in arr)
            req += x / m + (x % m > 0 ? 1 : 0);
         
        // If the required number of
        // arrays is more than K, it
        // means we need to increase
        // the value of P
        if (req > K)
            l = m + 1;
 
        else
        {
             
            // If the required number of
            // arrays is less than or equal
            // to K, it means this is a
            // possible answer and we go to
            // check if any smaller possible
            // value exists for P
            ans = m;
            r = m - 1;
        }
    }
    return ans;
}
     
// Driver Code
public static void Main(String[] args)
{
 
    // Given array
    int []arr = { 1, 2, 3, 4, 5 };
 
    // Number of available small arrays
    int K = 7;
 
    Console.Write(MinimumCapacity(arr, K));
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
    // JavaScript program to find the minimum
    // capacity of small arrays needed
    // to contain all element of
    // the given array
     
    // Function returns the value
    // of minimum capacity needed
    function MinimumCapacity(arr, K)
    {
 
        // Initializing maximum
        // value
        let maxVal = arr[0];
 
        // Finding maximum value
        // in arr
        for(let x = 0; x < arr.length; x++)
            maxVal = Math.max(maxVal, arr[x]);
 
        let l = 1, r = maxVal, m;
        let ans = 0, req;
 
        // Binary Search the answer
        while (l <= r)
        {
 
            // m is the mid-point
            // of the range
            m = l + parseInt((r - l) / 2, 10);
 
            req = 0;
 
            // Finding the total number of
            // arrays needed to completely
            // contain the items array
            // with P = req
            for(let x = 0; x < arr.length; x++)
                req +=
                parseInt(arr[x] / m, 10) + (arr[x] % m > 0 ? 1 : 0);
 
            // If the required number of
            // arrays is more than K, it
            // means we need to increase
            // the value of P
            if (req > K)
                l = m + 1;
 
            else
            {
 
                // If the required number of
                // arrays is less than or equal
                // to K, it means this is a
                // possible answer and we go to
                // check if any smaller possible
                // value exists for P
                ans = m;
                r = m - 1;
            }
        }
        return ans;
    }
     
    // Given array
    let arr = [ 1, 2, 3, 4, 5 ];
   
    // Number of available small arrays
    let K = 7;
   
    document.write(MinimumCapacity(arr, K));
 
</script>
Output: 
3

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :