Practice · 2 of 3
Longest Window with K Distinct
CHALLENGE
Difficulty: intermediate+25 XP
Implement static int longestWindow(int[] xs, int k) — the length
of the longest contiguous run containing at most k distinct values.
k <= 0 → 0. Use the sliding-window pattern with a frequency map.
[1,2,1,2,3], k=2 → 4 (the run [1,2,1,2]).
[1,1,1], k=1 → 3. Empty → 0.
Press Submit to check your solution.
Back to lesson: Practice: Algorithms Lab