Dsa
Sliding Window
Hard
Maximum Subarray Length with at Least K Distinct Integers
Given an integer array nums and an integer k, return the length of the longest subarray that contains at least k distinct integers. If no such subarray exists, return 0.
Input:
- The first line contains the integer n (1 ≤ n ≤ 10^5): the number of elements in the array.
- The second line contains n space-separated integers, representing the elements of the array nums.
- The third line contains the integer k (1 ≤ k ≤ n): the required number of distinct integers.
Output:
- A single integer representing the length of the longest subarray with at least k distinct integers.
Example:
Input:
7
1 2 1 2 3 1 2
2
Output:
5
Key concepts
sliding_windowarrayshash_maps
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