Data Structures & AlgorithmsModerateCodeforces Div2 C/D · Amazon SDE OA

Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code.

Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code.
print(sum(1 for a in range(1,21) for b in range(a+1,21) if a|b==31))
What is printed?
  1. A. 11
  2. B. 64
  3. C. 0
  4. D. 5

Correct answer: A. 11

Bitwise OR equals 31 only when the two values cover all five bits, and a < b excludes the equal pair. AND with zero is a different condition, AND equal to 31 is impossible for these values, and requiring a plus b to equal 31 is a different constraint.

More Coding Interview Problems questions

Exams that ask this topic