Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
What is the sum of the minimum element over all contiguous subarrays of [5, 3, 1, 4, 2]?
- A. 28
- B. 29
- C. 26
- D. 31
Correct answer: A. 28
For each element, count the subarrays where it is the minimum using its left and right boundaries from a monotonic stack. Summing the minimum of each subarray directly is the brute method.
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 containing only 1s in the binary matrix whose rows are 1101, 1111, 0111 and 1110?
- A grid has rows ..#. / .##. / .... / #.#. where '.' is free and '#' is a wall. Moving up, down, left or right one cell at a time, what is…
- Two sorted linked lists A = [1, 4, 6, 9] and B = [2, 3, 7, 10, 12] are merged with the standard two-pointer merge, which compares the…
- How many distinct non-empty palindromic subsequences does the string abcba have (distinct as strings, not as index sets)?
- What is the sum of gcd(i, 12) over i = 1 to 12?
- Five customers arrive together at a bank with 2 tellers. Service times in arrival order are 3, 5, 2, 4 and 6 minutes. Each customer goes…