2Brute2Best
🔥0

Count Subarrays Where Max Element Appears at Least K Times

Medium·LeetCode #2962 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given an integer array nums and a positive integer k.

Let mx be the largest value in the whole array. Count how many contiguous subarrays contain mx at least k times.

Input

An integer array nums and an integer k.

Output

The number of subarrays in which the maximum element of nums appears at least k times (as a 64-bit integer).

Example 1
Input: nums = [1, 3, 2, 3, 3], k = 2
Output: 6

The maximum is 3. The subarrays with at least two 3's are [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].

Example 2
Input: nums = [1, 4, 2, 1], k = 3
Output: 0

The maximum 4 appears only once in the whole array, so no subarray has it 3 times.

Constraints
  • • 1 ≤ nums.length ≤ 10^5
  • • 1 ≤ nums[i] ≤ 10^6
  • • 1 ≤ k ≤ 10^5
  • • The answer can reach n(n+1)/2 ≈ 5·10^9, so it needs long long.
02

Pattern Recognition Clues

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

“count subarrays”
→ Counting problem → add a block of subarrays for each right end.
“maximum element (of nums)”
→ One fixed value for the whole array. Find it first; then you only count its copies. No deque needed.
“appears at least k times”
→ An "at least" rule: growing keeps it true. Good starts form a prefix, so add `left` after shrinking.
03

Approach Complexity Ladder

Brute Force — first good end for every startO(N²)O(1)

For each start, grow until mx appears k times, then add n − end

Optimal — shrink while good, then add leftO(N)O(1)

Shrink while the window has k copies of mx; all starts before left work

Brute Force Intuition

Find mx. For each start, grow the end and count copies of mx. When the count reaches k, every longer subarray from this start also has at least k copies, so add n − end and move to the next start.

Recognition rule for this sub-pattern:

Count subarrays that satisfy a rule. "At most": for each right end, every start from left to right works, so add (right − left + 1). "At least": once the window is good, every longer one is too, so add left (or n − right).
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.