Maximum number of elements greater than X after equally distributing subset of array | C ++ implementation to find the maximum number of elements greater than X by equally distributing ; Function to find the maximum number of elements greater than X by equally distributing ; Sorting the array ; Loop to iterate over the elements of the array ; If no more elements can become larger than x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void redistribute ( int arr [ ] , int n , int x ) { sort ( arr , arr + n , greater < int > ( ) ) ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum / ( i + 1 ) < x ) { cout << i << endl ; break ; } } if ( i == n ) cout << n << endl ; } int main ( ) { int arr [ ] = { 5 , 1 , 2 , 1 } ; int x = 3 ; redistribute ( arr , 4 , x ) ; return 0 ; }