Distinct Elements in K-Sized Windows

The task is to determine the number of distinct elements in every sliding window of size 'K' across an array 'ARR' of size 'N'. A 'K' sized window is a contiguous sequence of 'K' elements in the array.

Input:

The first line contains an integer, 'T', representing the number of test cases.
For each test case:
First line contains two integers, 'N' and 'K', separated by a space, where 'N' is the size of the array and 'K' is the size of the window.
Second line contains 'N' integers representing the elements of the array 'ARR'.

Output:

For each test case, return an array detailing the count of distinct elements in each 'K' sized window as it slides across the array. Print each result on a new line.

Example:

Input:
ARR = [1, 2, 1, 3, 4, 2, 3], K = 3
Output:
[2, 3, 3, 3, 3]
Explanation:

Window-1: {1, 2, 1} - Distinct elements: {1, 2}
Window-2: {2, 1, 3} - Distinct elements: {2, 1, 3}
Window-3: {1, 3, 4} - Distinct elements: {1, 3, 4}
Window-4: {3, 4, 2} - Distinct elements: {3, 4, 2}
Window-5: {4, 2, 3} - Distinct elements: {4, 2, 3}

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 105
  • 1 ≤ K ≤ N
  • 1 ≤ ARR[i] ≤ 109
  • Time limit is 1 second.
Note:

The solution does not require printing results, as it should only return the calculated array for each test case.

AnswerBot
4mo

Calculate the count of distinct elements in each sliding window of size 'K' across an array 'ARR'.

  • Use a sliding window approach to iterate through the array and keep track of distinct elements using a...read more

Help your peers!
Select
Add answer anonymously...

WatchGuard Technologies Software Developer interview questions & answers

A Software Developer was asked 1mo agoQ. Given an array of integers, find the number of pairs whose sum is equal to a giv...read more
A Software Developer was asked Q. 5 dsa array string
A Software Developer was asked Q. MergeSort Linked List Problem Statement You are given a Singly Linked List of in...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. 5 dsa array string
A Software Developer was asked Q2. MergeSort Linked List Problem Statement You are given a Singly Linked List of in...read more
A Software Developer was asked Q3. Distinct Elements in K-Sized Windows The task is to determine the number of dist...read more
WatchGuard Technologies Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits