Length of Longest Subarray With at Most K Frequency - Problem

Imagine you're organizing a playlist where no song should play more than k times in any continuous segment. Given an integer array nums and an integer k, you need to find the longest contiguous subarray where each element appears at most k times.

The frequency of an element is how many times it occurs in the subarray. A subarray is considered "good" if every element in it has a frequency โ‰ค k.

Goal: Return the length of the longest good subarray.

Example: For nums = [1,2,3,1,2,3,1,2] and k = 2, the longest good subarray could be [1,2,3,1,2,3] with length 6, since each element (1, 2, 3) appears exactly 2 times.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,3,1,2,3,1,2], k = 2
โ€บ Output: 6
๐Ÿ’ก Note: The longest good subarray is [1,2,3,1,2,3] with length 6. Each element (1, 2, 3) appears exactly 2 times, which equals k=2.
example_2.py โ€” All Same Elements
$ Input: nums = [1,1,1,1,1], k = 3
โ€บ Output: 3
๐Ÿ’ก Note: Since all elements are 1, the longest subarray where 1 appears at most 3 times has length 3: [1,1,1].
example_3.py โ€” Single Element Array
$ Input: nums = [5], k = 1
โ€บ Output: 1
๐Ÿ’ก Note: The array has only one element, and its frequency (1) is โ‰ค k (1), so the answer is 1.

Visualization

Tap to expand
๐ŸŽต DJ's Playlist Optimization๐ŸŽต1๐ŸŽต2๐ŸŽต3๐ŸŽต1๐ŸŽต2๐ŸŽต3Valid Playlist Segment (Length = 6)๐ŸŽ›๏ธ Frequency CounterSong 1: 2Song 2: 2Song 3: 2โœ“ All songs โ‰ค k=2 plays๐ŸŽฏ Sliding Window: Expand right, shrink left when neededTime: O(n) | Space: O(k) where k is max distinct elements
Understanding the Visualization
1
Start the Playlist
Begin with an empty playlist segment and start adding songs
2
Track Song Plays
Keep a counter for how many times each song has played in current segment
3
Expand Safely
Add songs to your playlist as long as no song exceeds k plays
4
Shrink When Needed
When a song would exceed k plays, remove songs from the beginning until it's valid again
5
Remember the Best
Always remember the longest valid playlist segment you've created
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window technique allows us to find the optimal playlist segment in linear time by intelligently expanding and contracting our search window based on frequency constraints.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is visited at most twice (once by right pointer, once by left)

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Hash map stores at most k different elements in the current valid window

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 105
  • 1 โ‰ค k โ‰ค nums.length
  • k represents the maximum allowed frequency for any element
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
38.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen