Data Structures & AlgorithmsModerateICPC regional · GATE CS

Problem: How many distinct values does the expression (a² + b²) mod 5 take as a and b range over 0 ≤ a, b ≤ 4?

  1. A. 5
  2. B. 6
  3. C. 4
  4. D. 7

Correct answer: A. 5

Squares mod 5 are 0, 1 and 4, and sums of two of these cover every residue class mod 5. Counting only the squares of single values gives 3.

More Coding Interview Problems questions

Exams that ask this topic