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
def constructArray ( A , N ) : NEW_LINE INDENT left = [ 0 ] * ( N + 1 ) NEW_LINE right = [ 0 ] * ( N + 1 ) NEW_LINE X = [ 0 ] * ( N + 1 ) NEW_LINE Y = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT X [ i ] = left [ A [ i ] ] NEW_LINE left [ A [ i ] ] += 1 NEW_LINE DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT Y [ i ] = right [ A [ i ] ] NEW_LINE right [ A [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT print ( Y [ i ] - X [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 2 , 1 , 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE constructArray ( A , N ) NEW_LINE DEDENT