Find the minimum and maximum amount to buy all N candies | Java implementation to find the minimum and maximum amount ; 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
import java . io . * ; import java . util . Arrays ; import java . lang . Math ; class GFG { static void find ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; int b = ( int ) Math . ceil ( n / k * 1.0 ) ; int min_sum = 0 , max_sum = 0 ; for ( int i = 0 ; i < b ; i ++ ) min_sum += arr [ i ] ; for ( int i = 2 ; i < arr . length ; i ++ ) max_sum += arr [ i ] ; System . out . println ( " minimum ▁ " + min_sum ) ; System . out . println ( " maximum ▁ " + max_sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 4 } ; int n = arr . length ; int k = 2 ; find ( arr , n , k ) ; } }