Data Structures & AlgorithmsModerateTCS NQT Digital · CAT

Problem: Count the unordered pairs (x, y) with 1 ≤ x < y ≤ 7 for which x OR y equals 7.

  1. A. 24
  2. B. 12
  3. C. 3
  4. D. 13

Correct answer: B. 12

x | y = 7 means the pair covers bits 1, 2, 4; checking the 21 pairs finds 12 (every pair with 7, plus (3,4), (3,5), (5,6), etc.). Ordered pairs give 24, XOR = 7 gives 3, and allowing x = y adds (7,7) for 13.

More Coding Interview Problems questions

Exams that ask this topic