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.
- A. 4
- B. 6
- C. 1
- 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
- unsigned char a = 200, b = 100; unsigned char c = a + b; printf("%d", c); What is printed?
- In C99 and later, what are the values of (-7) / 2 and (-7) % 2?
- The modular inverse of 3 modulo 1000000007 is which value?
- Which expression is correct for (a b) % MOD with MOD = 1000000007 and 0 <= a, b < MOD, to avoid overflow in C?
- How many times does the loop x &= x - 1 run for x = 0b101100 before x becomes 0?
- For unsigned x = 40 (binary 101000), what is x & -x?