Maximum number of elements greater than X after equally distributing subset of array | Java 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
import java . util . * ; class GFG { static void redistribute ( Integer arr [ ] , int n , int x ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum / ( i + 1 ) < x ) { System . out . print ( i + "NEW_LINE"); break ; } } if ( i == n ) System . out . print ( n + "NEW_LINE"); } public static void main ( String [ ] args ) { Integer arr [ ] = { 5 , 1 , 2 , 1 } ; int x = 3 ; redistribute ( arr , 4 , x ) ; } }