AlgoInsight

DSA problem

Count Subarrays With Fixed Bounds

HardTwo PointersPattern: Two Pointers / Sliding Windowtop600AmazonGoogleMicrosoftAdobe

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

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

Return the number of fixed-bound subarrays.

A subarray is a contiguous part of an array.

Examples

Example 1
Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5
Output: 2
Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
Example 2
Input: nums = [1,1,1,1], minK = 1, maxK = 1
Output: 10
Explanation: Every subarray of nums is a fixed-bound subarray. There are 10 such subarrays.
Example 3
Input: nums = [4,2,7,3], minK = 2, maxK = 7
Output: 1
Explanation: Only the subarray [2,7] has minimum 2 and maximum 7.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

two-pointerssliding-windowarray

Browse all DSA problems