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));
}- A. 4
- B. 3
- C. 6
- 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
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3, 4, 5, 6}, best = 0; for (int i = 0; i + 3 <= 6; i++) { int s =…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (-17 / 5) 5 + (-17 % 5) == -17); }
- How many leading zero bits does the 16-bit value 0x0123 have? Count them with this program. c #include <stdint.h> #include <stdio.h> int…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { return atoi((const char const )a)…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = a, y = b; if (x[0] != y[0])…
- What is the digit sum of 2^15 = 32768? The recursion below computes it. Assume int is 32 bits. What is printed? c #include <stdio.h> int…