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.
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
Top WatchGuard Technologies Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month