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?
- A. 5
- B. 3
- C. 6
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A linked list holds 1, 2, 3, 4, 5, 6, 7. Reversing each complete group of 3 nodes (leaving a final incomplete group unchanged) gives which…
- How many integers from 1 to 100 inclusive are divisible by at least one of 3, 5 or 7?
- What is the sum of lcm(a, b) over all unordered pairs of distinct positions in [2, 3, 4, 6]?
- How many non-empty subsequences of [2, 3, 5, 7] have a product strictly less than 50?
- In how many ways can [1, 2, 3, 0, 3] be cut into 3 non-empty contiguous parts such that all three parts have equal sum?
- What is the length of the longest contiguous subarray of [1, 5, 6, 2, 8, 3] whose maximum minus minimum is at most 3?