Count pairs from an array having sum of twice of their AND and XOR equal to K | Java program for the above approach ; Function to count number of pairs satisfying the given conditions ; Stores the frequency of array elements ; Stores the total number of pairs ; Traverse the array ; Add it to cnt ; Update frequency of current array element ; Print the count ; Driver Code ; Given array ; Size of the array ; Given value of K
import java . io . * ; import java . util . * ; class GFG { static void countPairs ( int arr [ ] , int N , int K ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . get ( K - arr [ i ] ) != null ) cnt += mp . get ( K - arr [ i ] ) ; mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; } System . out . println ( cnt ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 4 , 8 , 7 } ; int N = arr . length ; int K = 9 ; countPairs ( arr , N , K ) ; } }