Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 hours
int 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 Code
int main()
{
    vector<int> arr = { 3, 6, 7, 11 };
    int H = 8;
 
    // Function Call
    minCollectingSpeed(arr, H);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
  
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;
  
    // 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 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 sanjoy_62


C#




// C# program for the above approach
using 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 hours
def 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 Code
if __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 hours
function 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 Code
var arr = [3, 6, 7, 11];
var H = 8;
// Function Call
minCollectingSpeed(arr, H);
 
 
</script>


Output: 

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



Last Updated : 08 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads