Maximize sum of second minimums of each K length partitions of the array | C ++ program for the above approach ; Function to find the maximum sum of second smallest of each partition of size K ; Sort the array A [ ] in ascending order ; Store the maximum sum of second smallest of each partition of size K ; Select every ( K - 1 ) th element as second smallest element ; Update sum ; Print the maximum sum ; Driver Code ; Given size of partitions ; Given array A [ ] ; Size of the given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSum ( int A [ ] , int N , int K ) { sort ( A , A + N ) ; int sum = 0 ; for ( int i = N / K ; i < N ; i += K - 1 ) { sum += A [ i ] ; } cout << sum ; } int main ( ) { int K = 4 ; int A [ ] = { 2 , 3 , 1 , 4 , 7 , 5 , 6 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findSum ( A , N , K ) ; return 0 ; }