Count array elements that can be maximized by adding any permutation of first N natural numbers | Java program for the above approach ; Function to get the count of values that can have the maximum value ; Sort array in decreasing order ; Stores the answer ; mark stores the maximum value till each index i ; Check if arr [ i ] can be maximum ; Update the mark ; Print the total count ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static void countMaximum ( Integer [ ] a , int n ) { Arrays . sort ( a , Collections . reverseOrder ( ) ) ; int count = 0 ; int mark = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( a [ i ] + n >= mark ) ) { count += 1 ; } mark = Math . max ( mark , a [ i ] + i + 1 ) ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 8 , 9 , 6 } ; int N = arr . length ; countMaximum ( arr , N ) ; } }