Maximize the number of indices such that element is greater than element to its left | C # 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
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int maxFreq = 0 ; foreach ( KeyValuePair < int , int > it in mp ) { maxFreq = Math . Max ( maxFreq , it . Value ) ; } Console . Write ( N - maxFreq + " STRNEWLINE " ) ; } 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 ) ; } }