Data Structures & AlgorithmsHardCodeforces Div2 D · TCS NQT
How many contiguous subarrays of [2, 5, 1, 5, 3, 4, 5] have a maximum element exactly equal to 5?
- A. 23
- B. 26
- C. 21
- D. 3
Correct answer: A. 23
Count subarrays with maximum ≤ 5 minus subarrays with maximum ≤ 4, or use the nearest-greater boundaries for each 5. Counting only the positions equal to 5 ignores subarrays that contain a 5 as their maximum with other values.
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 [3, 8, 1, 6, 9, 4], find the pair of distinct indices (i < j) with a[i] + a[j] = 10, minimising i + j. What is the minimum value of i +…
- How many DISTINCT sums appear among the windows of exactly 3 consecutive elements of [4, 1, 4, 2, 1, 4, 2]?
- How many contiguous subarrays of 4 2 6 2 5 3 2 7 contain EXACTLY one occurrence of the global minimum value 2?
- Add up the LENGTHS of all contiguous subarrays of 1 3 2 2 5 4 6 that are non-decreasing (each qualifying subarray contributes its length…
- How many contiguous subarrays of 5 2 7 3 9 4 1 of length at least 2 have pairwise DISTINCT values, and whose FIRST element is their maximum?
- What is the maximum sum of a contiguous subarray of 4 9 1 6 3 2 8 5 that has length at least 2 and whose sum is divisible by 5?