Data Structures & AlgorithmsHardGATE CS · TCS NQT

For an array of 10^5 integers you must count index pairs with a[i] XOR a[j] equal to a fixed K. Which method is the best fit?

  1. A. Sorting then two pointers, because XOR preserves order
  2. B. A hashmap of value counts, looking up a[i] XOR K for each element as you scan
  3. C. Segment tree over values, updating counts by range
  4. D. Floyd-Warshall on the array indices

Correct answer: B. A hashmap of value counts, looking up a[i] XOR K for each element as you scan

XOR with K is an involution, so each value's partner is a[i] XOR K and a single pass with counts gives all pairs. Order-based tools do not apply to XOR.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic