Maximizing the elements with a [ i + 1 ] > a [ i ] | C # implementation of the approach ; returns the number of positions where A ( i + 1 ) is greater than A ( i ) after rearrangement of the array ; Creating a HashMap containing char as a key and occurrences as a value ; Find the maximum frequency ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countMaxPos ( int [ ] arr ) { int n = arr . Length ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; foreach ( int x in arr ) { if ( map . ContainsKey ( x ) ) map [ x ] = map [ x ] + 1 ; else map . Add ( x , 1 ) ; } int max_freq = 0 ; foreach ( KeyValuePair < int , int > entry in map ) max_freq = Math . Max ( max_freq , entry . Value ) ; return n - max_freq ; } public static void Main ( String [ ] args ) { int [ ] arr = { 20 , 30 , 10 , 50 , 40 } ; Console . WriteLine ( countMaxPos ( arr ) ) ; } }