Data Structures & AlgorithmsHardGATE CS · TCS NQT
What is the sum of the maximum over all contiguous subarrays of [3, 1, 2, 4]?
- A. 31
- B. 30
- C. 28
- D. 17
Correct answer: B. 30
Each element is the maximum of the subarrays in which it is the largest, counted by a left and right span with a monotonic stack. Summing directly over all ten subarrays gives the same total.
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
- How many contiguous subarrays of [2, 1, 3, 4, 2] have a sum in the inclusive range [5, 7]?
- What is the number of 1-bits in the bitwise OR of 6, 10 and 12?
- For the array [1, -1, 2], how many distinct values appear among its prefix sums, counting the empty prefix 0?
- Four items of weight 4 each must be shipped in 2 boxes of equal capacity C (each box must hold whole items). What is the smallest feasible…
- What is the minimum absolute difference between two distinct elements of [12, 5, 9, 1, 15, 7]?
- How many distinct characters occur at least twice in "programming"?