Dsa
Sliding Window
Hard
Bounded-Mode Diversity
Find the length of the longest contiguous subarray that contains at most K distinct elements, and at least one of those elements appears at least M times within the subarray.
Input Format:
- Line 1: Two integers, K (max distinct) and M (min frequency of mode).
- Line 2: Integer N.
- Line 3: N space-separated integers.
Output Format:
- A single integer representing the maximum length.
Example:
Input:
2 3
7
1 2 1 2 1 3 3
Output:
5
(Explanation: Subarray [1, 2, 1, 2, 1] has 2 distinct elements {1, 2} and '1' appears 3 times. Length 5.)
Key concepts
sliding_windowhash_map
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now