Dsa
Sliding Window
Easy
The Rare Variety
An archaeologist is cataloging artifacts found in a trench. Each artifact has a type ID (integer). To ensure a representative sample for the museum, they need to find the shortest contiguous segment of artifacts that contains at least K distinct types of artifacts.
Input Format:
- The first line contains two integers: K (the number of distinct types required) and N (the total number of artifacts).
- The second line contains N space-separated integers representing the artifact type IDs.
Output Format:
- Print a single integer representing the length of the shortest segment. If no such segment exists, print 0.
Example:
Input:
3 6
1 2 1 3 2 3
Output:
3
Explanation: The segment [1, 3, 2] or [2, 1, 3] both have length 3 and contain 3 distinct types.
Key concepts
sliding_windowhash_mapfrequency_count
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