Find the Number of Subarrays Where Boundary Elements Are Maximum - Problem

Imagine you're analyzing data segments where you need to find special subarrays - those where the first and last elements are the rulers of their domain!

Given an array of positive integers nums, your task is to count how many subarrays have a unique property: the first and last elements of the subarray are equal to the largest element within that subarray.

What makes a subarray special?

  • The subarray's maximum value appears at both boundaries (first and last positions)
  • No element inside the subarray is larger than the boundary elements
  • Single-element subarrays automatically qualify (boundary = maximum)

Example: In [2, 4, 1, 4], the subarray [4, 1, 4] is special because both boundaries are 4 (the maximum), while [2, 4, 1] is not special because 2 โ‰  4 (the maximum).

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1, 4, 3, 4, 1]
โ€บ Output: 6
๐Ÿ’ก Note: Valid subarrays: [1], [4], [3], [4], [1], [4,3,4]. Each single element forms a valid subarray, and [4,3,4] is valid because both boundaries are 4 (the maximum).
example_2.py โ€” All Equal Elements
$ Input: nums = [3, 3, 3]
โ€บ Output: 6
๐Ÿ’ก Note: Valid subarrays: [3], [3], [3], [3,3], [3,3], [3,3,3]. When all elements are equal, every subarray satisfies the condition since all elements are maximum.
example_3.py โ€” Single Element
$ Input: nums = [5]
โ€บ Output: 1
๐Ÿ’ก Note: Only one subarray [5] exists, and it satisfies the condition since the single element is both the first, last, and maximum element.

Visualization

Tap to expand
๐Ÿฐ The Kingdom Watchtowers ProblemInput Array: [1, 4, 3, 4, 1]14341Valid Subarrays (Boundaries = Maximum):[1][4][3][4][1][4,3,4]Max=4, Boundaries: 4,4 โœ“๐ŸŽฏ Result: 6 valid subarrays found!Algorithm: Check each subarray O(nยฒ), find max O(n) โ†’ Total: O(nยณ)Optimization possible with monotonic stack โ†’ O(n)
Understanding the Visualization
1
Identify Subarrays
Generate all possible contiguous subarrays from the input array
2
Find Maximum
For each subarray, determine the maximum element value
3
Check Boundaries
Verify if first and last elements equal the maximum
4
Count Valid
Increment counter for each subarray meeting the criteria
Key Takeaway
๐ŸŽฏ Key Insight: Every single element forms a valid subarray, and multi-element subarrays are valid only when the maximum value appears at both boundaries, ensuring no internal element exceeds the guards.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is pushed and popped from stack at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for the monotonic stack in worst case

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • All elements are positive integers
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
27.3K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen