Count array elements that can be maximized by adding any permutation of first N natural numbers | 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 ; Prlet the total count ; Given array arr [ ] ; Function call
function countMaximum ( a , n ) { a . sort ( ) ; a . reverse ( ) ; let count = 0 ; let mark = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( ( a [ i ] + n >= mark ) ) { count += 1 ; } mark = Math . max ( mark , a [ i ] + i + 1 ) ; } document . write ( count ) ; } let arr = [ 8 , 9 , 6 ] ; let N = arr . length ; countMaximum ( arr , N ) ;