Count Bowl Subarrays

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
Inputnums = [2,5,3,1,4]
Output2
The bowl subarrays are [3, 1, 4] and [5, 3, 1, 4].
Example 2
Inputnums = [5,1,2,3,4]
Output3
The bowl subarrays are [5, 1, 2], [5, 1, 2, 3], and [5, 1, 2, 3, 4].

Constraints

  • 3 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • nums consists of distinct elements.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate