Count Subarrays With Fixed Bounds

You are given an integer array nums and two integers minK and maxK.

A fixed-bound subarray of nums is a subarray that satisfies the following conditions:

  • The minimum value in the subarray is equal to minK.
  • The maximum value in the subarray is equal to maxK.

Return the number of fixed-bound subarrays.

A subarray is a contiguous part of an array.

Example 1
Inputnums = [1,3,5,2,7,5], minK = 1, maxK = 5
Output2
The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
Example 2
Inputnums = [1,1,1,1], minK = 1, maxK = 1
Output10
Every subarray of nums is a fixed-bound subarray, so there are 10 possible subarrays.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i], minK, maxK <= 10^6

Asked at 9 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