text: Minimum number of coins to be collected per hour to empty N piles in at most H hours | C # 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 ; Stores the minimum coins to be removed per hour ; Find the maximum array element ; Perform Binary Search ; Store the mid value of the range in K ; Find the total time taken to empty N piles by removing K coins per hour ; If total time does not exceed H ; Otherwise ; Print the required result ; Driver Code ; Function Call
code: 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 ) ; } }