Store the sum of frequency of elements greater than the current element
int x = 0 ; for ( Map . Entry < Integer , Integer > e : mp . entrySet ( ) ) { Integer temp = e . getValue ( ) ; mp . put ( e . getKey ( ) , x ) ; x += temp ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( mp . get ( arr [ i ] ) + " ▁ " ) ; }