Question bank › Sliding Window
Dsa Sliding Window Easy

Count Distinct Elements in All Windows

Given an array of integers and an integer 'k', compute the number of distinct elements in every contiguous subarray of size 'k'. ### Input Format: - The first line contains integers n, k (1 <= n <= 1000, 1 <= k <= n). - The second line contains n space-separated integers representing the elements of the array. ### Output Format: - Output k integers, where each integer is the count of distinct elements in the corresponding subarray. ### Example: Input: 6 3 1 2 1 2 3 1 Output: 2 2 3 ### Explanation: 1st subarray: [1, 2, 1] -> 2 distinct elements (1,2) 2nd subarray: [2, 1, 2] -> 2 distinct elements (1,2) 3rd subarray: [1, 2, 3] -> 3 distinct elements (1,2,3)

Key concepts

sliding_windowdictionaryhash_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
Part of Praxari's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.