Data Structures & AlgorithmsHard
What is the sum, over all contiguous subarrays of [3, 1, 3, 2], of the maximum element of each subarray?
- A. 33
- B. 27
- C. 21
- D. 16
Correct answer: B. 27
Each subarray must be credited to exactly one position of its maximum. Left side stops at a value ≥ a[i] and right side stops at a value > a[i], which gives the unique owner when two equal maxima exist.
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
- In [4, 7, 5, 9, 6, 3], how many contiguous subarrays have (max − min) ≤ 3?
- How many contiguous subarrays of [1, 2, 1, 3, 3, 2, 2, 1] contain at most 2 distinct values?
- For the array [1, 3, 2, 6, 7, 4, 8, 12, 5], how many index pairs i < j have (J[i] + J[j]) divisible by 4?
- In the binary array [1, 0, 0, 1, 0, 1, 1, 0, 0, 0], how many contiguous subarrays contain equal numbers of 0s and 1s?
- What is the length of the longest contiguous subarray of [2, -4, 3, 1, 6, -2, 5, 4] whose sum is divisible by 6?
- How many contiguous subarrays of [1, 2, 1, 3, 4, 2, 3] contain no repeated value?