Count pairs from an array having sum of twice of their AND and XOR equal to K | C # 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
using System ; using System . Collections . Generic ; public class GFG { static void countPairs ( int [ ] arr , int N , int K ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( K - arr [ i ] ) ) cnt += mp [ K - arr [ i ] ] ; if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } Console . WriteLine ( cnt ) ; } static public void Main ( ) { int [ ] arr = { 1 , 5 , 4 , 8 , 7 } ; int N = arr . Length ; int K = 9 ; countPairs ( arr , N , K ) ; } }