Find the minimum and maximum amount to buy all N candies | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find ( vector < int > arr , int n , int k ) { sort ( arr . begin ( ) , arr . end ( ) ) ; int b = 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 . size ( ) ; i ++ ) max_sum += arr [ i ] ; cout << " minimum ▁ " << min_sum << endl ; cout << " maximum ▁ " << max_sum << endl ; } int main ( ) { vector < int > arr = { 3 , 2 , 1 , 4 } ; int n = arr . size ( ) ; int k = 2 ; find ( arr , n , k ) ; }