Count pairs from an array whose Bitwise OR is greater than Bitwise AND | C ++ program for the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Total number of pairs possible from the array ; Stores frequency of each array element ; Traverse the array A [ ] ; Increment ump [ A [ i ] ] by 1 ; Traverse the Hashmap ump ; Subtract those pairs ( i , j ) from count which has the same element on index i and j ( i < j ) ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int A [ ] , int n ) { long long count = ( n * ( n - 1 ) ) / 2 ; unordered_map < long long , long long > ump ; for ( int i = 0 ; i < n ; i ++ ) { ump [ A [ i ] ] ++ ; } for ( auto it = ump . begin ( ) ; it != ump . end ( ) ; ++ it ) { long long c = it -> second ; count = count - ( c * ( c - 1 ) ) / 2 ; } cout << count ; } int main ( ) { int A [ ] = { 1 , 4 , 7 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; countPairs ( A , N ) ; return 0 ; }