Data Structures & AlgorithmsVery hard
How many contiguous subarrays of 3 1 4 1 5 9 2 6 contain the position of the global maximum value 9?
- A. 14
- B. 17
- C. 18
- D. 19
Correct answer: C. 18
A subarray contains the position of the maximum when its left end is at or before it and its right end is at or after it, giving (pos+1)·(n−pos) blocks. Subtracting one removes a block that actually qualifies; using the minimum or excluding singletons gives other totals.
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
- For each contiguous window of size 3 in 4 2 12 3 8 1 7, take its minimum. What is the largest of these window minimums?
- For every contiguous window of size 3 in 4 2 12 3 8 1 7, take its minimum and add all these minimums. What is the total?
- How many contiguous windows of length exactly 4 in 2 5 1 0 3 4 2 6 have a sum strictly less than 10?
- What is the length of the longest contiguous subarray of 3 4 4 5 3 2 6 6 7 in which every two elements differ by at most 1, i.e. max − min…
- How many contiguous windows of length 3 in 1 3 2 4 6 5 7 9 are strictly increasing?
- How many contiguous subarrays of length at least 2 of 5 1 2 1 6 2 have a MAXIMUM element strictly greater than the sum of all their other…