Dsa
Arrays
Easy
Count Distinct Elements in Every Subarray of Size K
You are given an array of integers and an integer k. Count the number of distinct integers in every contiguous subarray of length k.
Input format:
- The first line contains an integer n (1 <= n <= 10^5), the number of elements in the array.
- The second line contains n space-separated integers (|a[i]| <= 1000), the elements of the array.
- The third line contains an integer k (1 <= k <= n).
Output format:
- Output k space-separated integers, where the ith integer represents the count of distinct elements in the subarray starting from index i.
Example:
Input:
5
1 2 1 3 4
3
Output:
2 3 3
Key concepts
arrayshashmap
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