↓Skip to main content
  1. LeetCode/

LeetCode 2537: Count the Number of Good Subarrays

·2 mins· ·
LeetCode Medium Sliding-Window Hash-Table 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-20

  • Submissions: Wrong Answer ×2 → Accepted ×2

Approach
#

Equal-value pairs in the window: adding an x pairs it with every x already there, so the pair count rises by cnt[x] (the count before adding). Removing reverses it, falling by cnt[x] - 1 (the count after removal).

The pair count only grows with the window, so “at least k pairs” is monotonic: use the “at least” count, shrinking while valid and adding left.

My Attempts
#

I started with combinations: on adding, subtract the old C(cnt - 1, 2) and add the new C(cnt, 2).

VersionProblem
Attempt 1The combinations used freq[nums[right - 1]] (the previous element’s count) instead of freq[nums[right]]. For nums = [2, 2, 3], k = 1, the answer is 2; it returned 4
Attempt 2Fixed the index, but when shrinking it updated the pair count without decrementing freq[nums[left]]. For nums = [3, 2, 2, 3], k = 1, the answer is 4; it returned 5
Attempt 3, ACAdded freq[nums[left]] -= 1
Attempt 4, ACSwitched to “add cnt[x] on entry, subtract cnt[x] on exit”, much simpler

Solution
#

class Solution:
    def countGood(self, nums: List[int], k: int) -> int:
        cnt = Counter()
        pairs = left = count = 0
        for x in nums:
            pairs += cnt[x]                        # pairs with every x already in the window
            cnt[x] += 1
            while pairs >= k:
                y = nums[left]
                cnt[y] -= 1
                pairs -= cnt[y]                    # loses its pairs with the remaining y's
                left += 1
            count += left
        return count

Complexity
#

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

Mistakes to Avoid
#

  • Updating the statistic while shrinking but forgetting the count table.
  • Using the wrong element in an incremental update (nums[right - 1] vs nums[right]).

Interview Explanation
#

Adding an x pairs it with every x already in the window, so the pair count grows by that count, and removal reverses it. Pairs only increase as the window grows, so I use an “at least” sliding window: while there are at least k pairs I shrink the left, and every left end from 0 to left - 1 is valid, so I add left. Time O(n).

Related

LeetCode 2875: Minimum Size Subarray in Infinite Array
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 2875 note: strip target // sum full cycles; the remainder is smaller than one cycle, so a sliding window over two copies of nums always finds it
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies
LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 3795 note: a shortest-window slide where each value counts once; track each value’s last position, and subtract the left value only if it was the last occurrence
LeetCode 3679: Minimum Discards to Balance Inventory
·3 mins
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 note: the rule already decides what must be discarded, so simulate it; the length-w window counts only kept arrivals, and discarded ones must not be subtracted when they leave
LeetCode 2841: Maximum Sum of Almost Unique Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2841 note: a fixed window of length k keeps a sum and a count map; the number of distinct values is the map’s size, so delete keys that drop to zero
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map