Minimum number of coins to be collected per hour to empty N piles in at most H hours
Last Updated :
08 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int minCollectingSpeed(vector< int >& piles,
int H)
{
int ans = -1;
int low = 1, high;
high = *max_element(piles.begin(),
piles.end());
while (low <= high)
{
int K = low + (high - low) / 2;
int time = 0;
for ( int ai : piles) {
time += (ai + K - 1) / K;
}
if ( time <= H) {
ans = K;
high = K - 1;
}
else {
low = K + 1;
}
}
cout << ans;
}
int main()
{
vector< int > arr = { 3, 6, 7, 11 };
int H = 8;
minCollectingSpeed(arr, H);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minCollectingSpeed( int [] piles,
int H)
{
int ans = - 1 ;
int low = 1 , high;
high = Arrays.stream(piles).max().getAsInt();
while (low <= high)
{
int K = low + (high - low) / 2 ;
int time = 0 ;
for ( int ai : piles)
{
time += (ai + K - 1 ) / K;
}
if (time <= H)
{
ans = K;
high = K - 1 ;
}
else
{
low = K + 1 ;
}
}
System.out.print(ans);
}
static public void main(String args[])
{
int [] arr = { 3 , 6 , 7 , 11 };
int H = 8 ;
minCollectingSpeed(arr, H);
}
}
|
C#
using System;
using System.Collections;
class GFG
{
static void minCollectingSpeed( int [] piles,
int H)
{
int ans = -1;
int low = 1, high;
Array.Sort(piles);
high = piles[piles.Length - 1 ];
while (low <= high)
{
int K = low + (high - low) / 2;
int time = 0;
foreach ( int ai in piles)
{
time += (ai + K - 1) / K;
}
if (time <= H)
{
ans = K;
high = K - 1;
}
else
{
low = K + 1;
}
}
Console.Write(ans);
}
static public void Main( string []args)
{
int [] arr = { 3, 6, 7, 11 };
int H = 8;
minCollectingSpeed(arr, H);
}
}
|
Python3
def minCollectingSpeed(piles, H):
ans = - 1
low = 1
high = max (piles)
while (low < = high):
K = low + (high - low) / / 2
time = 0
for ai in piles:
time + = (ai + K - 1 ) / / K
if (time < = H):
ans = K
high = K - 1
else :
low = K + 1
print (ans)
if __name__ = = '__main__' :
arr = [ 3 , 6 , 7 , 11 ]
H = 8
minCollectingSpeed(arr, H)
|
Javascript
<script>
function minCollectingSpeed(piles, H)
{
var ans = -1;
var low = 1, high;
high = piles.reduce((a,b)=> Math.max(a,b));
while (low <= high)
{
var K = low + parseInt((high - low) / 2);
var time = 0;
piles.forEach(ai => {
time += parseInt((ai + K - 1) / K);
});
if (time <= H) {
ans = K;
high = K - 1;
}
else {
low = K + 1;
}
}
document.write( ans);
}
var arr = [3, 6, 7, 11];
var H = 8;
minCollectingSpeed(arr, H);
</script>
|
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
Share your thoughts in the comments
Please Login to comment...