2Brute2Best
🔥0

Count Subarrays With Fixed Bounds

Hard·LeetCode #2444 Pro OnlyTricky / Edge Case
LeetCode
01

Problem Statement

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

A contiguous subarray is a fixed-bound subarray if its smallest value is exactly minK and its largest value is exactly maxK.

Count the fixed-bound subarrays.

This is a LeetCode Hard. It sits at position 4 of the Exactly K sub-pattern because the usual atMost − atMost trick does not fit it directly.

Input

An integer array nums and two integers minK and maxK.

Output

The number of subarrays whose minimum is minK and whose maximum is maxK. The count can be large, so it is returned as a 64-bit number.

Example 1
Input: nums = [1, 3, 5, 2, 7, 5], minK = 1, maxK = 5
Output: 2

The fixed-bound subarrays are [1,3,5] and [1,3,5,2]. Anything that includes the 7 has max 7, and [5] alone has min 5, not 1.

Example 2
Input: nums = [1, 1, 1, 1], minK = 1, maxK = 1
Output: 10

Every subarray has min 1 and max 1. There are 4·5/2 = 10 subarrays.

Constraints
  • • 2 ≤ nums.length ≤ 10^5
  • • 1 ≤ nums[i], minK, maxK ≤ 10^6
  • • The answer can exceed the int range — use a 64-bit type.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“count subarrays”
→ For each right end, count a block of good starts.
“minimum is exactly minK, maximum is exactly maxK”
→ "Exactly" again, but on two values at once. It splits into: no value outside the range (monotonic → window) + contains a minK and a maxK (last-seen index).
“values outside the range”
→ Walls. They reset the window: left edge = last wall + 1.
03

Approach Complexity Ladder

Brute Force — try every startO(N²)O(1)

Fix each start, grow the end, track min and max

Optimal — last bad, last min, last maxO(N)O(1)

For each right, good starts are lastBad+1 … min(lastMin, lastMax)

Brute Force Intuition

For every start, move the end right while keeping the running minimum and maximum. If an out-of-range value shows up, stop — every longer subarray contains it too. Count one whenever min = minK and max = maxK.

Recognition rule for this sub-pattern:

"Exactly K" is not monotonic, so a window can't count it directly. Use exactly(K) = atMost(K) − atMost(K − 1).
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.