Difference of count of distinct elements present to left and right for each array element | C ++ program for the above approach ; Function to find the difference of count of distince elements to the left and right for each array elements ; Stores distinct array element in the left and right ; Traverse the array ; Insert all element to the left in the set S1 ; Insert all element to the right in the set S2 ; Print the difference ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void findDifferenceArray ( int arr [ ] , int N ) { set < int > S1 ; set < int > S2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { S1 . insert ( arr [ j ] ) ; } for ( int j = i + 1 ; j < N ; j ++ ) { S2 . insert ( arr [ j ] ) ; } cout << abs ( ( int ) S1 . size ( ) - ( int ) S2 . size ( ) ) << ' ▁ ' ; S1 . clear ( ) ; S2 . clear ( ) ; } } int main ( ) { int arr [ ] = { 7 , 7 , 3 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findDifferenceArray ( arr , N ) ; return 0 ; }