Skip to main content

Count Bowl Subarrays - Solution & Explanation

MediumArrayStackMonotonic Stack3 min readAsked at: Amazon, Google
Practice this problem

Problem Statement

You are given an integer array nums with distinct elements.

A subarray nums[l...r] of nums is called a bowl if:

  • The subarray has length at least 3. That is, r - l + 1 >= 3.
  • The minimum of its two ends is strictly greater than the maximum of all elements in between. That is, 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 <= 105
  • 1 <= nums[i] <= 109
  • nums consists of distinct elements.

Approach Overview

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.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n^3)O(1)Understanding the definition of a bowl and validating small inputs
Expand Around MiddleO(n^2)O(1)Moderate constraints where checking outward from the valley is feasible
Monotonic Stack BoundariesO(n)O(n)Optimal solution for large arrays; computes nearest greater elements efficiently

Video Solution

Count Bowl Subarrays | LeetCode 3676 | Weekly Contest 466 • Sanyam IIT Guwahati • 2,500 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Count Bowl Subarrays easy or hard?
Count Bowl Subarrays is generally considered a medium-level problem. The difficulty comes from recognizing that the bowl structure can be counted using nearest greater boundaries and implementing the monotonic stack correctly.
How to solve Count Bowl Subarrays in O(n)?
Compute the nearest greater element to the left and right for every index using a decreasing monotonic stack. Treat each index as the bowl bottom and use those boundaries to determine how many valid left and right endpoints exist. Combining those counts gives the number of bowl subarrays with that bottom.
Count Bowl Subarrays Python or Java solution
Most implementations follow the same pattern: compute nearest greater indices using a stack, then count valid subarray boundaries for each index acting as the bowl bottom. The logic is identical across Python, Java, C++, and Go with only syntax differences.
What is the best approach for Count Bowl Subarrays?
The most efficient approach uses a monotonic stack to find the nearest greater element on both sides of every index. Treat each element as the bottom of a potential bowl and count valid boundaries using those nearest-greater limits. This reduces the runtime to O(n) with O(n) extra space.
What data structure is used in Count Bowl Subarrays?
The key data structure is a monotonic stack. It maintains elements in decreasing order so you can quickly determine the nearest greater element on each side of an index while scanning the array once.
What is the time complexity of Count Bowl Subarrays?
The optimal solution runs in O(n) time because each array element is pushed to and popped from the monotonic stack at most once. Brute-force approaches range from O(n^2) to O(n^3) depending on how the subarrays are validated.
Is Count Bowl Subarrays asked at Google, Amazon, or Meta?
Problems involving monotonic stacks and boundary detection frequently appear in interviews at companies like Amazon, Google, and Meta. Variants of subarray counting and nearest greater element problems are especially common in array-focused interview rounds.

Ready to solve this problem?

Practice Count Bowl Subarrays with our built-in code editor and test cases.

Practice on FleetCode