Choose n elements such that their mean is maximum | C ++ implementation of the approach ; Utility function to print the contents of an array ; Function to print the array with maximum mean ; Sort the original array ; Construct new array ; Print the resultant array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void printMaxMean ( int arr [ ] , int n ) { int newArr [ n ] ; sort ( arr , arr + 2 * n ) ; for ( int i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } int main ( ) { int arr [ ] = { 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printMaxMean ( arr , n / 2 ) ; return 0 ; }