Data Structures & AlgorithmsHardHCLTech · Wipro pattern
What is the sum of the minimum element over every contiguous subarray of [7, 6, 6, 4, 8, 9]?
- A. 106
- B. 110
- C. 156
- D. 126
Correct answer: B. 110
Using a monotonic stack, each a[i] contributes a[i] × (elements between it and the previous smaller) × (elements between it and the next smaller-or-equal). Summing this (or the direct brute force over all 21 subarrays) gives 110. 156 is the sum of the *maximum* of each subarray instead, a different quantity.
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
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "BABAACBCCA" are anagrams of "AB"?
- A circular queue of capacity 5 starts empty and runs this sequence in order: enqueue(7), enqueue(2), dequeue(), enqueue(5), dequeue()…
- What is the postfix (Reverse Polish) form of the infix expression "f + d h -(i + e)", built with the standard stack-based conversion (^…
- Keys 88, 61, 14, 52, 18 are inserted via linear probing (h(k) = k mod 7, probe by +1 on collision) into an initially empty table of size 7…
- What is the postfix (Reverse Polish) form of the infix expression "j + g h -(i + c)", built with the standard stack-based conversion (^…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 2, 1, 0, 0, 0, 2, 2] (values 0, 1, 2 only) in…