# Count The Number Of Good Subarrays: Explained

Count The Number Of Good Subarrays shows up often in coding rounds. Here is how I talk through it when the interviewer is watching.

## Key idea

A subarray is good if it has at least k pairs of equal elements. Maintain a frequency map in a sliding window and a running pair-count (each new occurrence of x adds freq[x]); grow/shrink to the shortest prefix that is already good and add the left options.

## Steps to say out loud

1. Restate the input and the exact ask, and nail down constraints and overflow.
2. Name the pattern (binary search on the answer, monotonic stack or deque, interval / interval-DP, string hashing, graph traversal, or shortest paths) and why a nested scan or naive search fails.
3. Code it, then trace a short example plus one empty or already-solved edge case.

## Complexity

O(n).

## Common mistake

Defining a pair as any two indices rather than equal values, or using combinations C(freq,2) recomputed from scratch every step.

## Practice tip

Time yourself: two minutes to explain, ten to code, three to test edges. Disclosure: I work on Linkjob AI. I also keep a practice cheat sheet at AI tool for job interviews.