Count pairs from an array having sum of twice of their AND and XOR equal to K | 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 ; Given array ; Size of the array ; Given value of K
function countPairs ( arr , N , K ) { let mp = new Map ( ) ; let cnt = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp . has ( K - arr [ i ] ) ) { cnt += mp . get ( K - arr [ i ] ) ; } if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } document . write ( cnt ) ; } let arr = [ 1 , 5 , 4 , 8 , 7 ] ; let N = arr . length ; let K = 9 ; countPairs ( arr , N , K ) ;