↓Skip to main content
  1. LeetCode/

LeetCode 2962: Count Subarrays Where Max Element Appears at Least K Times

·2 mins· ·
LeetCode Medium Sliding-Window Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-19

  • Submissions: Accepted (first try)

Approach
#

mx is the maximum of the whole array and never changes. More copies of mx in the window only help, so “at least k times” is monotonic: a valid window stays valid when extended left.

The “at least K” counting pattern:

  • Add on the right; while mx appears k times, keep shrinking the left until it just drops below k.
  • Then any left end in [0, left) gives a subarray with at least k copies of mx, so count += left.

My Solution
#

class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        mx = max(nums)
        left = seen = count = 0
        for x in nums:
            seen += x == mx
            while seen >= k:                       # shrink while valid, until just invalid
                seen -= nums[left] == mx
                left += 1
            count += left                          # left ends in [0, left) are valid
        return count

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Using right - left + 1: that is the “at most” count; this one is “at least”.
  • The maximum is that of the whole array, not the window.

Interview Explanation
#

The maximum is fixed as the whole array’s maximum, and appearing at least k times is monotonic. I slide a window and, while the max appears k times, keep shrinking the left until it is just short; then every left end from 0 to left - 1 works, so I add left. Time O(n).

Related

LeetCode 1040: Moving Stones Until Consecutive II
·3 mins
LeetCode Medium Sliding-Window Math Sorting Array
LeetCode 1040 note: max moves = total gaps minus the edge gap the first move wastes; min moves uses a length-n window holding the most stones, with a special case of 2 when n - 1 stones are already consecutive and one is far away
LeetCode 2555: Maximize Win From Two Segments
·2 mins
LeetCode Medium Sliding-Window Binary-Search Array
LeetCode 2555 note: a sliding window gives how many prizes a segment ending at each prize collects, and a running “best single segment among the first i prizes” supplies the second segment to its left
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 2958: Length of Longest Subarray With at Most K Frequency
·1 min
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2958 note: a longest-window slide that shrinks the left whenever the newly added number appears more than k times
LeetCode 3634: Minimum Removals to Balance Array
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3634 note: after sorting, what remains is always a contiguous block; for each minimum nums[left], a right-only pointer finds how far it can extend, and everything outside is removed