Maximize the number of indices such that element is greater than element to its left | Function to find the maximum pairs such that arr [ i + 1 ] > arr [ i ] ; To store the frequency of the element in arr [ ] ; Store the frequency in map M ; To find the maximum frequency store in map M ; Print the maximum number of possible pairs ; Driver Code
function countPairs ( arr , N ) { let M = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( M [ arr [ i ] ] ) M [ arr [ i ] ] ++ ; else M [ arr [ i ] ] = 1 } let maxFreq = 0 ; for ( let it in M ) { maxFreq = Math . max ( maxFreq , M [ it ] ) ; } document . write ( N - maxFreq , ' ' ) ; } let a = [ 1 , 8 , 5 , 9 , 8 , 8 , 7 , 7 , 5 , 7 , 7 ] ; let N = a . length ; countPairs ( a , N ) ;