Generate array having differences between count of occurrences of every array element on its left and right | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArray ( int A [ ] , int N ) { int left [ N + 1 ] = { 0 } ; int right [ N + 1 ] = { 0 } ; int X [ N + 1 ] = { 0 } , Y [ N + 1 ] = { 0 } ; 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 ++ ) { cout << Y [ i ] - X [ i ] << " ▁ " ; } } int main ( ) { int A [ ] = { 3 , 2 , 1 , 2 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; constructArray ( A , N ) ; return 0 ; }