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
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] A , int n ) { long count = ( n * ( n - 1 ) ) / 2 ; Dictionary < long , long > ump = new Dictionary < long , long > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ump . ContainsKey ( A [ i ] ) ) { ump [ A [ i ] ] ++ ; } else { ump [ A [ i ] ] = 1 ; } } foreach ( KeyValuePair < long , long > it in ump ) { long c = it . Value ; count = count - ( c * ( c - 1 ) ) / 2 ; } Console . WriteLine ( count ) ; } static void Main ( ) { int [ ] A = { 1 , 4 , 7 } ; int N = A . Length ; countPairs ( A , N ) ; } }