Find the minimum and maximum amount to buy all N candies | C # implementation to find the minimum and maximum amount ; Function to find the minimum amount to buy all candies ; Buy current candy ; And take k candies for free from the last ; Function to find the maximum amount to buy all candies ; Buy candy with maximum amount ; And get k candies for free from the starting ; Driver code ; Function call
using System ; public class GFG { static int findMinimum ( int [ ] arr , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += arr [ i ] ; n = n - k ; } return res ; } static int findMaximum ( int [ ] arr , int n , int k ) { int res = 0 , index = 0 ; for ( int i = n - 1 ; i >= index ; i -- ) { res += arr [ i ] ; index += k ; } return res ; } public static void Main ( ) { int [ ] arr = { 3 , 2 , 1 , 4 } ; int n = arr . Length ; int k = 2 ; Array . Sort ( arr ) ; Console . WriteLine ( findMinimum ( arr , n , k ) + " ▁ " + findMaximum ( arr , n , k ) ) ; } }