Maximizing the elements with a [ i + 1 ] > a [ i ] | Java 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
import java . util . * ; class GFG { static int countMaxPos ( int [ ] arr ) { int n = arr . length ; HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int x : arr ) { if ( map . containsKey ( x ) ) map . put ( x , map . get ( x ) + 1 ) ; else map . put ( x , 1 ) ; } int max_freq = 0 ; for ( Map . Entry entry : map . entrySet ( ) ) max_freq = Math . max ( max_freq , ( int ) entry . getValue ( ) ) ; return n - max_freq ; } public static void main ( String [ ] args ) { int [ ] arr = { 20 , 30 , 10 , 50 , 40 } ; System . out . println ( countMaxPos ( arr ) ) ; } }