Data Structures & AlgorithmsHardCodeforces Div2 B · recurrence

Problem: What does the Josephus recurrence J(1)=0, J(i)=(J(i-1)+2) mod i give for n = 7 (answer 1-indexed, every second person eliminated)? Run the code.

Problem: What does the Josephus recurrence J(1)=0, J(i)=(J(i-1)+2) mod i give for n = 7 (answer 1-indexed, every second person eliminated)? Run the code.
J = 0
for i in range(2, 8):
    J = (J + 2) % i
print(J + 1)
What is printed?
  1. A. 7
  2. B. 6
  3. C. 5
  4. D. 3

Correct answer: A. 7

With n = 7 = 4 + 3 and k = 2, the survivor is 2L + 1 = 2 * 3 + 1 = 7. The trap 3 comes from the 0-indexed value confusion.

More Coding Interview Problems questions

Exams that ask this topic