Watch 10 video solutions for Count Bowl Subarrays, a medium level problem involving Array, Stack, Monotonic Stack. This walkthrough by Sanyam IIT Guwahati has 2,394 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums with distinct elements.
A subarray nums[l...r] of nums is called a bowl if:
r - l + 1 >= 3.min(nums[l], nums[r]) > max(nums[l + 1], ..., nums[r - 1]).Return the number of bowl subarrays in nums.
Example 1:
Input: nums = [2,5,3,1,4]
Output: 2
Explanation:
The bowl subarrays are [3, 1, 4] and [5, 3, 1, 4].
[3, 1, 4] is a bowl because min(3, 4) = 3 > max(1) = 1.[5, 3, 1, 4] is a bowl because min(5, 4) = 4 > max(3, 1) = 3.Example 2:
Input: nums = [5,1,2,3,4]
Output: 3
Explanation:
The bowl subarrays are [5, 1, 2], [5, 1, 2, 3] and [5, 1, 2, 3, 4].
Example 3:
Input: nums = [1000000000,999999999,999999998]
Output: 0
Explanation:
No subarray is a bowl.
Constraints:
3 <= nums.length <= 1051 <= nums[i] <= 109nums consists of distinct elements.Problem Overview: You are given an array and must count subarrays that form a bowl shape. A valid bowl has a lower middle element with larger values on both sides, creating a valley-like structure inside the subarray.
Approach 1: Brute Force Enumeration (O(n^3) time, O(1) space)
The most direct approach checks every possible subarray [l, r] and verifies whether it forms a bowl. For each pair of indices, iterate through the subarray to locate the minimum element and confirm that elements toward the left decrease toward it while elements toward the right increase away from it. This requires nested loops plus a validation scan, leading to O(n^3) time. It is rarely acceptable for large inputs but helps clarify the bowl definition.
Approach 2: Improved Scan with Precomputed Checks (O(n^2) time, O(1) space)
Instead of validating the entire subarray each time, fix the middle index as the potential bowl bottom and expand outward. While expanding left and right, ensure the left side strictly decreases toward the bottom and the right side strictly increases away from it. Each expansion validates potential boundaries and counts valid subarrays. This reduces redundant scanning compared with brute force but still requires checking many index pairs, resulting in O(n^2) time.
Approach 3: Monotonic Stack Boundary Analysis (O(n) time, O(n) space)
The optimal method uses a monotonic stack to quickly determine the nearest greater elements on both sides of every index. Treat each element as the potential bottom of a bowl. Using a decreasing stack, compute the nearest greater element to the left and the nearest greater element to the right. These boundaries represent the closest positions where the bowl walls can form.
Once these boundaries are known, count how many valid left endpoints exist between the bottom and its left boundary, and how many right endpoints exist between the bottom and its right boundary. The total bowls using that index as the bottom are derived from these combinations. The stack ensures each index is pushed and popped once, giving linear runtime.
This pattern appears frequently in array problems that rely on stack-based boundary discovery or monotonic ordering. Similar logic is used in histogram area problems and subarray minimum/maximum calculations over arrays.
Recommended for interviews: Interviewers expect the monotonic stack solution. Starting with brute force demonstrates understanding of the bowl definition, but the linear-time boundary computation shows strong algorithmic reasoning and familiarity with stack-based array techniques.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n^3) | O(1) | Understanding the definition of a bowl and validating small inputs |
| Expand Around Middle | O(n^2) | O(1) | Moderate constraints where checking outward from the valley is feasible |
| Monotonic Stack Boundaries | O(n) | O(n) | Optimal solution for large arrays; computes nearest greater elements efficiently |