Problem · Stack

Count Bowl Subarrays

Learn this problem
MediumPhonePeONSITE INTERVIEW

Problem statement

You are given an integer array nums with distinct elements.

A subarray nums[l...r] is a bowl when:

  • Its length is at least 3.
  • The smaller of its two endpoint values is strictly greater than every value between the endpoints.

Return the number of bowl subarrays.

Function

bowlSubarrays(nums: int[]) → long

Examples

Example 1

nums = [2,5,3,1,4]return = 2

The bowl subarrays are [3,1,4] and [5,3,1,4].

Example 2

nums = [5,1,2,3,4]return = 3

The valid 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 contains distinct values.

More PhonePe problems

drafts saved locally
public long bowlSubarrays(int[] nums) {
  // write your code here
}
nums[2,5,3,1,4]
expected2
checking account