Dsa
Sliding Window
Easy
Count Distinct Elements in Subarrays
You are given an array of integers and an integer k. Calculate the count of distinct integers in every contiguous subarray of size k. Return an array containing these counts.
Input:
- The first line contains two integers, n (the size of the array) and k (the size of the subarray).
- The second line contains n space-separated integers representing the elements of the array.
Output:
- Output a list of integers, where each integer represents the count of distinct elements in a subarray.
Example:
Input:
8 3
1 2 1 3 4 2 3 3
Output:
2 3 3 2 2
Key concepts
sliding_windowhashmapcounting
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