Count of pairs from Array with sum equal to twice their bitwise AND | Java implementation to find the pairs with equal sum and twice the bitwise AND of the pairs ; Map to store the occurrence of elements of array ; Function to find the pairs with equal sum and twice the bitwise AND of the pairs ; Loop to find the frequency of elements of array ; Function to find the count such pairs in the array ; If an element occurs more than once then the answer will by incremented by nC2 times ; Driver Code ; Function Call
import java . util . * ; class GFG { static HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; static int find_pairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { int count = i . getValue ( ) ; if ( count > 1 ) { ans += ( ( count * ( count - 1 ) ) / 2 ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 5 , 7 , 8 } ; int arr_size = arr . length ; System . out . print ( find_pairs ( arr , arr_size ) ) ; } }