Count of greater elements for each element in the Array | Java implementation of the above approach ; Store the frequency of the array elements ; Store the sum of frequency of elements greater than the current element ; Driver code
import java . util . * ; class GfG { public static void countOfGreaterElements ( int arr [ ] ) { int n = arr . length ; TreeMap < Integer , Integer > mp = new TreeMap < Integer , Integer > ( Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( arr [ i ] , mp . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } 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 ] ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 9 , 5 , 2 , 1 , 3 , 4 , 8 , 6 } ; countOfGreaterElements ( arr ) ; } }