ProgrammingVery hardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function J(n, k)
    If n = 1 Then Return 0
    Return (J(n - 1, k) + k) mod n
End Function
Print J(13, 4) + 1
  1. A. 4
  2. B. 13
  3. C. 9
  4. D. 5

Correct answer: D. 5

J(n,k) is the 0-based position of the survivor when every k-th person is removed from a circle of n. Unwinding J(1) = 0, J(2) = 0 + 4 mod 2, and so on up to n = 13 gives J(13,4) = 4, so the survivor is person 5 in 1-based numbering (the printed value adds 1).

More Pseudocode and Code Tracing questions

Exams that ask this topic