Generate array having differences between count of occurrences of every array element on its left and right | Java program of the above approach ; Function to construct array of differences of counts on the left and right of the given array ; Initialize left and right frequency arrays ; Construct left cumulative frequency table ; Construct right cumulative frequency table ; Print the result ; Driver Code ; Function Call
import java . io . * ; class GFG { static void constructArray ( int A [ ] , int N ) { int [ ] left = new int [ N + 1 ] ; int [ ] right = new int [ N + 1 ] ; int [ ] X = new int [ N + 1 ] ; int [ ] Y = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { X [ i ] = left [ A [ i ] ] ; left [ A [ i ] ] ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { Y [ i ] = right [ A [ i ] ] ; right [ A [ i ] ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( Y [ i ] - X [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 3 , 2 , 1 , 2 , 3 } ; int N = A . length ; constructArray ( A , N ) ; } }