Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

n = 7 people stand in a circle numbered 1 to 7. Starting from person 1, every 3rd remaining person is removed until one remains. Which person survives?

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

Correct answer: D. 4

The recurrence J(n, k) = (J(n-1, k) + k) mod n with J(1) = 0 gives the 0-based survivor, which shifts by 1 when reported with 1-based numbering. Forgetting the shift is the trap.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic