Mean of given array after removal of K percent of smallest and largest array elements | C ++ program for the above approach ; Function to calculate the mean of a given array after removal of Kth percent of smallest and largest array elements ; Sort the array ; Find the K - th percent of the array size ; Traverse the array ; Skip the first K - th percent & last K - th percent array elements ; Mean of the rest of elements ; Print mean upto 5 decimal places ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void meanOfRemainingElements ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int kthPercent = ( N * K ) / 100 ; float sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( i >= kthPercent && i < ( N - kthPercent ) ) sum += arr [ i ] ; float mean = sum / ( N - 2 * kthPercent ) ; cout << fixed << setprecision ( 5 ) << mean << endl ; } int main ( ) { int arr [ ] = { 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 5 ; meanOfRemainingElements ( arr , arr_size , K ) ; return 0 ; }