Problem Statement
How to count unique pairs in an array whose XOR equals a given value K? Outline an efficient method.
Explanation
Use a hash map or set: For each number x you want y = x ^ K. If y in set and pair-not-seen then count it. Mark x and y as used to avoid duplicates. Overall O(n) time, O(n) space. Bit-wise XOR property exploited instead of sum. Useful when K is large and sum collisions differ.
Code Solution
SolutionRead Only
Set<Integer> seen=new HashSet<>(); int count=0; for(int x:arr){ int y = x ^ K; if(seen.contains(y)){ count++; } seen.add(x); } return count;