ProgrammingHardGATE CS · Campus placements

Seven people stand in a circle numbered 1 to 7. Starting from person 1, every third person is removed until one remains. Which number survives? This is the Josephus problem with k = 3.

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

Correct answer: A. 4

The removal order is 3, 6, 2, 7, 5, 1, leaving 4. The recurrence J(n) = (J(n-1) + k) mod n gives the same answer, which is the O(n) method that contests expect.

More C Programming questions

Exams that ask this topic