Data Structures & AlgorithmsVery hardGATE CS
For the array [3, 1, 2, 4, 1, 5, 2, 6], what is the sum of the minimum element over all contiguous subarrays?
- A. 24
- B. 50
- C. 56
- D. 61
Correct answer: C. 56
Count how many subarrays each element is the minimum of (the nearest smaller element on the left, nearest smaller or equal on the right) and weight by value; the total is 56. Handling the duplicate 1 inconsistently would double count subarrays containing both 1s.
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 non-empty contiguous subarrays of [5, 2, -3, 8, 6, -9, 4, 7, 1, 3, 12, -5, 6, 2] have a sum divisible by 7?
- The infix expression ab^c^d-(e+fg)^h/i+j (^ is right-associative with highest precedence, then and /, then + and -, all others…
- For the pattern P = "abaabaabaabab", the KMP failure (prefix) function pi[i] is the length of the longest proper prefix of P[0..i] that is…
- The naive algorithm searches for P = "aaaab" in T = "aaabaaabaaaab": for each shift s = 0, 1, ..., it compares P[0], P[1], ... with T[s]…
- A queue is built from two stacks: an enqueue pushes onto the input stack; a dequeue pops from the output stack, and if the output stack is…
- From the digit string 1432219836475, exactly 5 digits are deleted (the remaining digits keep their order, and leading zeros are not an…