Introduction
LeetCode 2958: Length of Longest Subarray With at Most K Frequency is an excellent problem to understand how sliding window with frequency counting works in arrays with duplicates.
The problem asks us to find the longest contiguous subarray such that no element occurs more than K times.
If you’d like to try solving the problem first, you can attempt it here:
Try the problem on LeetCode:
https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/
Problem Understanding
You are given:
- An integer array
nums - An integer
k
A good subarray is defined as a subarray where the frequency of each element is ≤ k.
Goal: Return the length of the longest good subarray.
Examples:
A naive approach would check all subarrays and count frequencies →
- Time Complexity: O(n²) → too slow for
nums.lengthup to 10⁵
Key Idea: Sliding Window with Frequency Map
Instead of brute-force:
- Use a sliding window
[i, j]to track the current subarray - Use a HashMap
mpto store the frequency of each element in the window - Expand
jby addingnums[j]to the map - If
mp.get(nums[j]) > k, shrink the window from the left (i) untilmp.get(nums[j]) <= k - At each step, update the maximum length of the valid window
Intuition:
- We allow each element to appear at most K times
- By shrinking the window whenever a frequency exceeds
k, we always maintain a valid subarray - Sliding window ensures linear traversal
Approach (Step-by-Step)
- Initialize
i = 0,j = 0→ window pointers - Initialize
co = 0→ maximum length - Initialize HashMap
mpto store frequencies of elements in the window - Iterate
jfrom 0 tonums.length - 1: - Increment frequency of
nums[j]inmp - While
mp.get(nums[j]) > k: - Shrink window from the left by decrementing
mp[nums[i]]and incrementingi - Update
co = max(co, j - i + 1) - Return
coas the length of the longest good subarray
Implementation (Java)
Dry Run Example
Input:
Execution:
Window [i, j] | Frequency Map mp | Valid? | Max length co |
| [0,0] → [1] | {1:1} | Yes | 1 |
| [0,1] → [1,2] | {1:1,2:1} | Yes | 2 |
| [0,2] → [1,2,3] | {1:1,2:1,3:1} | Yes | 3 |
| [0,3] → [1,2,3,1] | {1:2,2:1,3:1} | Yes | 4 |
| [0,4] → [1,2,3,1,2] | {1:2,2:2,3:1} | Yes | 5 |
| [0,5] → [1,2,3,1,2,3] | {1:2,2:2,3:2} | Yes | 6 |
| [0,6] → [1,2,3,1,2,3,1] | {1:3,2:2,3:2} | No → shrink | 6 |
Output:
Complexity Analysis
- Time Complexity: O(n) → Each element enters and leaves the window at most once
- Space Complexity: O(n) → HashMap stores frequencies of elements in the window (worst-case all distinct)
Edge Cases Considered
- All elements occur ≤ k → entire array is valid
- All elements occur > k → max subarray length = k
- Single-element arrays → return 1 if k ≥ 1
- Large arrays up to 10⁵ elements → O(n) solution works efficiently
Sliding Window Pattern Importance
This problem demonstrates sliding window with frequency map, useful for:
- Subarrays with frequency constraints
- Longest subarray with limited duplicates
- Similar to "max consecutive ones with flips" and "fruit into baskets"
By mastering this approach, you can efficiently solve many array and subarray problems with constraints.
Conclusion
Using HashMap + sliding window, we reduce a naive O(n²) approach to O(n) while keeping the solution intuitive and easy to implement.
Understanding this pattern allows solving complex frequency-constrained subarray problems efficiently in interviews.




