Data Structures & AlgorithmsVery hardCodeforces Div2 C · GATE CS
For the array [4, 2, 5, 3, 1, 6, 0], a 'next greater element' routine returns for each index the value of the next strictly larger element to its right, or −1 if none. A buggy version scans forward for a larger element, and if none is found it returns a[i+1] whenever a[i+1] < a[i]. At which 0-based index does the buggy output first differ from the correct output? Answer −1 if they agree everywhere.
- A. 6
- B. 5
- C. 4
- D. 7
Correct answer: B. 5
The buggy fallback invents a value when no larger element exists, so the first divergence happens at an index whose next greater element does not exist. A correct monotone stack leaves −1 there.
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 routine builds a list t: for each x in a, it appends x to t only if x is larger than the last element of t (otherwise it skips x), and…
- Items as (weight, value) are [(2, 3), (3, 4), (1, 1)]. The 0/1 knapsack table dp[c] for capacities c = 0..6 is built correctly by scanning…
- Intervals [(1, 6), (2, 4), (3, 9), (5, 7), (8, 10), (4, 5)] are given as closed ranges. You must remove the fewest intervals so that no…
- How many non-empty contiguous subarrays of [1, -1, 2, 0, 1, 3, -2, 1] have a sum equal to their length (number of elements)? Transform…
- Choose any subset of the positions of [4, -2, 7, 3, 9, 1, -5, 8, 2] (possibly empty) so that every two chosen positions are at least 3…
- Assertion (A): Dynamic programming over subsets for TSP needs exponential time in the number of cities. Reason (R): The number of subsets…