Generate array having differences between count of occurrences of every array element on its left and right | 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
function constructArray ( A , N ) { let left = [ ] ; let right = [ ] ; let X = [ ] ; let Y = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { X [ i ] = 0 ; left [ i ] = 0 ; right [ i ] = 0 ; Y [ i ] = 0 ; } for ( let i = 0 ; i < N ; i ++ ) { X [ i ] = left [ A [ i ] ] ; left [ A [ i ] ] ++ ; } for ( let i = N - 1 ; i >= 0 ; i -- ) { Y [ i ] = right [ A [ i ] ] ; right [ A [ i ] ] ++ ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( Y [ i ] - X [ i ] + " " ) ; } } let A = [ 3 , 2 , 1 , 2 , 3 ] ; let N = A . length ; constructArray ( A , N ) ;