Data Structures & AlgorithmsHardCodeforces Div2 B · recurrence

Problem: What is the 1-indexed survivor when n = 6 people stand in a circle and every 3rd person is removed? Run the recurrence code.

Problem: What is the 1-indexed survivor when n = 6 people stand in a circle and every 3rd person is removed? Run the recurrence code.
J = 0
for i in range(2, 7):
    J = (J + 3) % i
print(J + 1)
What is printed?
  1. A. 4
  2. B. 5
  3. C. 1
  4. D. 2

Correct answer: C. 1

The recurrence J(i) = (J(i-1) + 3) mod i gives 0 for n = 6, so the 1-indexed survivor is person 1.

More Coding Interview Problems questions

Exams that ask this topic