ProgrammingVery hardCompetitive coding (CP)

Seven people stand in a circle and every third person is removed until one is left. The recursion J(n, k) gives the 1-based survivor position. What is printed?

Seven people stand in a circle and every third person is removed until one is left. The recursion J(n, k) gives the 1-based survivor position. What is printed?
#include <stdio.h>
int J(int n, int k) {
    if (n == 1) return 1;
    return (J(n - 1, k) + k - 1) % n + 1;
}
int main(void) {
    printf("%d", J(7, 3));
}
  1. A. 4
  2. B. 3
  3. C. 6
  4. D. 7

Correct answer: A. 4

The recurrence J(n, k) = (J(n - 1, k) + k - 1) mod n + 1 with J(1) = 1 gives position 4 for seven people and step 3.

More C Programming questions

Exams that ask this topic