Distributed C candies among N boys such that difference between maximum and minimum candies received is K | Function to calculate the maximum and minimum number of candies a boy can possess ; All candies will be given to one boy ; All the candies will be given to 1 boy ; Give K candies to 1 st boy initially ; Count remaining candies ; If the last candy of remaining candies is given to the last boy , i . e Nth boy ; Increase minimum count ; Driver code
function max_min ( N , C , K ) { let maximum , minimum ; if ( N == 1 ) { maximum = minimum = C ; } else if ( K >= C ) { maximum = C ; minimum = 0 ; } else { maximum = K ; minimum = 0 ; let remain_candy = C - K ; maximum += Math . floor ( remain_candy / N ) ; minimum = Math . floor ( remain_candy / N ) ; if ( remain_candy % N == N - 1 ) { minimum ++ ; } } document . write ( " " + maximum + " " ) ; document . write ( " " + minimum ) ; } let N = 4 ; let C = 12 ; let K = 3 ; max_min ( N , C , K ) ;