Maximize the number of indices such that element is greater than element to its left | Java program of the above approach ; 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
import java . util . * ; class GFG { static void countPairs ( int arr [ ] , int N ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int maxFreq = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { maxFreq = Math . max ( maxFreq , it . getValue ( ) ) ; } System . out . print ( N - maxFreq + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 5 , 9 , 8 , 8 , 7 , 7 , 5 , 7 , 7 } ; int N = arr . length ; countPairs ( arr , N ) ; } }