Minimum number of coins to be collected per hour to empty N piles in at most H hours

Given an array arr[] consisting of N integers representing the number of coins in each pile, and an integer H, the task is to find the minimum number of coins that must be collected from a single pile per hour such that all the piles are emptied in less than H hours.Â
Note: Coins can be collected only from a single pile in an hour.
Examples:
Input: arr[] = {3, 6, 7, 11}, H = 8
Output: 4
Explanation:Â
Removing 4 coins per pile in each hour, the time taken to empty each pile are as follows:Â
arr[0] = 3: Emptied in 1 hour.Â
arr[1] = 6: 4 coins removed in the 1st hour and 2 removed in the 2nd hour. Therefore, emptied in 2 hours.Â
arr[2] = 7: 4 coins removed in the 1st hour and 3 removed in the 2nd hour. Therefore, emptied in 2 hours.Â
arr[3] = 11: 4 coins removed in both 1st and 2nd hour, and 3 removed in the 3rd hour. Therefore, emptied in 3 hours.Â
Therefore, number of hours required = 1 + 2 + 2 + 3 = 8 ( = H).Input: arr[] = {30, 11, 23, 4, 20}, H = 5
Output: 30
Approach: The idea is to use Binary Search. Follow the steps below to solve the problem:
- Initialize a variable, say ans, to store the minimum number coins required to be collected per hour.
- Initialize variables low and high, as 1 and the maximum value present in the array, to set the range to perform Binary Search.
- Iterate until low ? high and perform the following steps:
- Find the value of mid as (low + high)/2.
- Traverse the array arr[] to find the time taken to empty all the pile of coins by removing mid coins per hour and check if the total time exceeds H or not. If found to be false, update the high to (K – 1) and update ans to K. Otherwise, update low to (K + 1).
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to find the minimum number// of coins to be collected per hour// to empty N piles in H hoursint minCollectingSpeed(vector<int>& piles,                       int H){    // Stores the minimum coins    // to be removed per hour    int ans = -1;Â
    int low = 1, high;Â
    // Find the maximum array element    high = *max_element(piles.begin(),                        piles.end());Â
    // Perform Binary Search    while (low <= high)Â
    {        // Store the mid value of the        // range in K        int K = low + (high - low) / 2;Â
        int time = 0;Â
        // Find the total time taken to        // empty N piles by removing K        // coins per hour        for (int ai : piles) {Â
            time += (ai + K - 1) / K;        }Â
        // If total time does not exceed H        if (time <= H) {            ans = K;            high = K - 1;        }Â
        // Otherwise        else {            low = K + 1;        }    }Â
    // Print the required result    cout << ans;}Â
// Driver Codeint main(){Â Â Â Â vector<int> arr = { 3, 6, 7, 11 };Â Â Â Â int H = 8;Â
    // Function Call    minCollectingSpeed(arr, H);Â
    return 0;} |
Java
// Java program for the above approachimport java.util.*;  class GFG{     // Function to find the minimum number// of coins to be collected per hour// to empty N piles in H hoursstatic void minCollectingSpeed(int[] piles,                               int H){         // Stores the minimum coins    // to be removed per hour    int ans = -1;      int low = 1, high;      // Find the maximum array element    high = Arrays.stream(piles).max().getAsInt();      // Perform Binary Search    while (low <= high)    {                 // Store the mid value of the        // range in K        int K = low + (high - low) / 2;          int time = 0;          // Find the total time taken to        // empty N piles by removing K        // coins per hour        for(int ai : piles)         {            time += (ai + K - 1) / K;        }          // If total time does not exceed H        if (time <= H)         {            ans = K;            high = K - 1;        }          // Otherwise        else        {            low = K + 1;        }    }      // Print the required result    System.out.print(ans);}  // Driver Codestatic public void main(String args[]){    int[] arr = { 3, 6, 7, 11 };    int H = 8;         // Function Call    minCollectingSpeed(arr, H);}}Â
// This code is contributed by sanjoy_62 |
C#
// C# program for the above approachusing System;using System.Collections;Â
class GFG{Â
  // Function to find the minimum number  // of coins to be collected per hour  // to empty N piles in H hours  static void minCollectingSpeed(int[] piles,                                 int H)  {Â
    // Stores the minimum coins    // to be removed per hour    int ans = -1;       int low = 1, high;        Array.Sort(piles);Â
    // Find the maximum array element    high = piles[piles.Length - 1 ];Â
    // Perform Binary Search    while (low <= high)    {Â
      // Store the mid value of the      // range in K      int K = low + (high - low) / 2;Â
      int time = 0;Â
      // Find the total time taken to      // empty N piles by removing K      // coins per hour      foreach(int ai in piles)       {        time += (ai + K - 1) / K;      }Â
      // If total time does not exceed H      if (time <= H)       {        ans = K;        high = K - 1;      }Â
      // Otherwise      else      {        low = K + 1;      }    }Â
    // Print the required result    Console.Write(ans);  }Â
  // Driver Code  static public void Main(string []args)  {    int[] arr = { 3, 6, 7, 11 };    int H = 8;Â
    // Function Call    minCollectingSpeed(arr, H);  }}Â
// This code is contributed by AnkThon |
Python3
# Python3 program for the above approachÂ
# Function to find the minimum number# of coins to be collected per hour# to empty N piles in H hoursdef minCollectingSpeed(piles, H):         # Stores the minimum coins    # to be removed per hour    ans = -1    low = 1Â
    # Find the maximum array element    high = max(piles)         # Perform Binary Search    while (low <= high):                 # Store the mid value of the        # range in K        K = low + (high - low) // 2Â
        time = 0Â
        # Find the total time taken to        # empty N piles by removing K        # coins per hour        for ai in piles:          time += (ai + K - 1) // KÂ
        # If total time does not exceed H        if (time <= H):            ans = K            high = K - 1Â
        # Otherwise        else:            low = K + 1Â
    # Print required result    print(ans)Â
# Driver Codeif __name__ == '__main__':Â Â Â Â arr = [3, 6, 7, 11]Â Â Â Â H = 8Â
    # Function Call    minCollectingSpeed(arr, H)Â
# This code is contributed by mohit kumar 29 |
Javascript
<script>Â
// Javascript program for the above approachÂ
// Function to find the minimum number// of coins to be collected per hour// to empty N piles in H hoursfunction minCollectingSpeed(piles, H){    // Stores the minimum coins    // to be removed per hour    var ans = -1;Â
    var low = 1, high;Â
    // Find the maximum array element    high = piles.reduce((a,b)=> Math.max(a,b));Â
    // Perform Binary Search    while (low <= high)Â
    {        // Store the mid value of the        // range in K        var K = low + parseInt((high - low) / 2);Â
        var time = 0;Â
        // Find the total time taken to        // empty N piles by removing K        // coins per hour        piles.forEach(ai => {             Â
            time += parseInt((ai + K - 1) / K);        });Â
        // If total time does not exceed H        if (time <= H) {            ans = K;            high = K - 1;        }Â
        // Otherwise        else {            low = K + 1;        }    }Â
    // Print the required result    document.write( ans);}Â
// Driver Codevar arr = [3, 6, 7, 11];var H = 8;// Function CallminCollectingSpeed(arr, H);Â
Â
</script> |
4
Â
Time Complexity: O(H*log N) // time complexity of binary search algorithm is logarithmic so the algorithm runs in logarithmic time
Auxiliary Space: O(1) // since no extra array is used hence space required by the algorithm is constant
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



