Find the minimum and maximum amount to buy all N candies | function to find the maximum and the minimum cost required ; Sort the array ; print the minimum cost ; print the maximum cost ; Driver Code ; Function call
function find ( arr , n , k ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let b = Math . floor ( Math . ceil ( n / k ) ) ; let min_sum = 0 , max_sum = 0 ; for ( let i = 0 ; i < b ; i ++ ) min_sum += arr [ i ] ; for ( let i = 2 ; i < arr . length ; i ++ ) max_sum += arr [ i ] ; document . write ( " " + min_sum + " " ) ; document . write ( " " + max_sum + " " ) ; } let arr = [ 3 , 2 , 1 , 4 ] ; let n = arr . length ; let k = 2 ; find ( arr , n , k ) ;