Count smaller elements present in the array for each array element | C # program for the above approach ; Function to count for each array element , the number of elements that are smaller than that element ; Stores the frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Compute prefix sum of the array hash [ ] ; Traverse the array arr [ ] ; If current element is 0 ; Print the resultant count ; Driver Code
using System ; class GFG { static void smallerNumbers ( int [ ] arr , int N ) { int [ ] hash = new int [ 100000 ] ; for ( int i = 0 ; i < N ; i ++ ) hash [ arr [ i ] ] ++ ; for ( int i = 1 ; i < 100000 ; i ++ ) { hash [ i ] += hash [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { Console . WriteLine ( "0" ) ; continue ; } Console . Write ( hash [ arr [ i ] - 1 ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 4 , 1 , 1 , 2 } ; int N = arr . Length ; smallerNumbers ( arr , N ) ; } }