Data Structures & AlgorithmsVery hardCodeforces Div2 B · GATE CS
In the Josephus game, 41 people stand in a circle numbered 1 to 41. Starting from person 1, every 3rd person is removed (counting continues around the circle). Which person number survives?
- A. 32
- B. 30
- C. 62
- D. 31
Correct answer: D. 31
Simulating with a queue (rotate k-1 times, then remove the front) is O(nk) and is the reliable check; off-by-one in the rotation count shifts the answer by one.
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
- What is the area of the largest rectangle that fits in a histogram whose bar heights are 2, 1, 5, 6, 2, 3 (each bar has width 1)?
- Rain falls on the elevation map [4, 2, 0, 3, 2, 5] (each bar has width 1). How many units of water are trapped in total?
- How many contiguous subarrays of [1, 2, 1, 2, 3, 3, 4] contain at most 2 distinct values?
- How many non-empty contiguous subarrays of [10, 5, 2, 6] have a product strictly less than 100?
- An LRU cache holds 3 keys. Requests arrive in the order 1, 2, 3, 1, 4, 2, 5, 1, 2, 3, 4, 5, and a request for an absent key is a miss that…
- Given the linked list 1 -> 2 -> ... -> 8, the list is reversed in consecutive groups of 3 (a final group with fewer than 3 nodes is left…