Count pairs from an array whose Bitwise OR is greater than Bitwise AND | Java 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
import java . util . * ; class GFG { static void countPairs ( int A [ ] , int n ) { int count = ( n * ( n - 1 ) ) / 2 ; Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( A [ i ] ) ) { mp . put ( A [ i ] , mp . get ( A [ i ] ) + 1 ) ; } else { mp . put ( A [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { int c = it . getValue ( ) ; count = count - ( c * ( c - 1 ) ) / 2 ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 4 , 7 } ; int N = A . length ; countPairs ( A , N ) ; } }