Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
A queue holds 1 to 6 in order. The front element is moved to the back, then the front is removed, repeated until one element remains. Which element survives?
- A. 6
- B. 3
- C. 5
- D. 7
Correct answer: C. 5
This is the Josephus problem with step 2; the survivor is at 2 * (6 - 2^2) + 1 = 5 for n = 6 with the standard formula, so tracing the queue confirms it.
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 number of integer pairs (x, y) with 1 <= x <= y <= 20 for which gcd(x, y) = 1 and x + y is even?
- How many elements of the array [6, 12, 18, 24, 30] are divisible by at least one element that appears before them in the array?
- From [4, -2, 7, 1, -5, 3, 8, 2], choose a window of 4 consecutive elements, then remove exactly one element from that window. What is the…
- How many index pairs i < j in [5, 2, 8, 3, 1, 9] satisfy a[i] a[j] <= 15?
- What is the sum of the minimum element over all contiguous subarrays of [5, 3, 4, 2] that have length at least 2?
- Meetings are half-open intervals [s, e) listed as (1,4), (2,5), (3,6), (7,9), (8,10), (5,7). A meeting ending at time t and another…