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

Find the smallest positive integer x such that x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7). Solve by brute-force search.

Find the smallest positive integer x such that x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7). Solve by brute-force search.
print(next(x for x in range(1,10**6) if x%3==2 and x%5==3 and x%7==2))
What is printed?
  1. A. 98
  2. B. 23
  3. C. 8
  4. D. 7

Correct answer: B. 23

The residues fix x modulo 105, and the unique residue class satisfying all three is found by search. Requiring x mod 7 = 0 changes the class, dropping the mod 7 condition gives the smallest x with only two residues, and adding the residues is not a valid method.

More Coding Interview Problems questions

Exams that ask this topic