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
6h

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!
Add answer anonymously...
WatchGuard Technologies Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter