Count Distinct Elements In Every Window

Moderate
0/80
Average time to solve is 15m
profile
Contributed by
15 upvotes
Asked in companies
HSBCZS AssociatesCIS - Cyber Infrastructure

Problem statement

Given an array of integers ‘arr’ of size ‘n’ and an integer ‘k’. You need to find the count of distinct elements in every sub-array of size ‘k’ in the given array. Return an integer array ‘count’ that consists of n - k + 1 integers where ‘count[i]’ is equal to the number of distinct elements in a sub-array of size ‘k’ starting from index ‘i’.

Note:
1. The sub-array of an array is a continuous part of the array.
2. Consider ‘0’ based indexing.
3. ‘k’ will always be less than or equal to ‘n’.
3. Don’t print anything, just return the integer array ‘count’.
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

The next ‘2*T’ lines represent the ‘T’ test cases.

The first line of each test case contains two space-separated integers ‘n’ and ‘k’  respectively representing the size of array ‘arr’ and size of sub-array to be considered.

The second line of the test case contains ‘n’ space-separated integers representing elements of the array ‘arr’.
Output format :
For each test case, return an integer array ‘count’ that consists of n - k + 1 integers where ‘count[i]’ is equal to the number of distinct elements in a sub-array of size ‘k’ starting from index ‘i’ in array ‘arr’.

Output for each query must be printed in a new line.
Constraints:
1 <= T <= 50
1 <= n <= 10^4
1 <= k <= n
-10^9 <= arr[i] <= 10^9

Where ‘T’ is the total number of test cases, ‘n’ is the given positive integer, ‘k’ is the size of sub-array to be considered, and arr[i] is the value of the array elements.


Time limit: 1 sec
Sample Input 1:
2
4 3
2 2 1 3
1 1
5
Sample Output 1:
2 3 
1
Explanation of Sample Input 1:
Test case 1:
Here, ‘n’ = 4, ‘k’ = 3 and ‘arr’ = {2, 2, 1, 3};
The sub-array of size 3 starting from index 0 is {2, 2, 1} and there are 2 distinct elements i.e  2 and 1 in it.
The sub-array of size 3 starting from index 1 is {2, 1, 3} and there are 3 distinct elements i.e 2, 1, and 3 in it.
So, the ‘count’ array will be {2, 3}.     

Test case 2:
Here, ‘n’ = 1, ‘k’ = 1 and ‘arr’ = {1};
The sub-array of size 1 starting from index 0 is {5} and there is 1 distinct element i.e 5 in it.
So, the ‘count’ array will be {1}.
Sample Input 2:
 2
 3 3
 1 1 1
 3 1
 1 2 2
Sample Output 2:
 1
 1 1 1 
Explanation of Sample Input 2:
Test case 1:
Here, ‘n’ = 3, ‘k’ = 3 and ‘arr’ = {1, 1, 1};
The sub-array of size 3 starting from index 0 is {1, 1, 1} and there is 1 distinct element i.e 1 in it.

Test case 2:
Here, ‘n’ = 3, k = ‘1’ and ‘arr’ = {1, 2, 2};
The sub-array of size 1 starting from index 0 is {1} and there is 1 distinct element i.e 1 in it.
The sub-array of size 1 starting from index 1 is {2} and there is 1 distinct element i.e 2 in it.
The sub-array of size 1 starting from index 2 is {2} and there is 1 distinct element i.e 2 in it.
So, the ‘count’ array will be {1, 1, 1}.
Hint

Iterate over all sub-arrays of size ‘k’.

Approaches (2)
Brute Force.
  • Make an integer array ‘count’ of size n-k+1.
  • Run a loop where ‘i’ ranges from 0 to n-k and for each ‘i’ count number of distinct elements in sub-array starting from this index, this can be done as follow.
    • Initialize an integer variable ‘distinct’:= 0.
    • Run a loop where ‘j’ ranges from ‘i’ to ‘i + k -1’ and for each ‘j’ check whether there exist any element in the range [i, j-1] which is equal ‘arr[j]’. If no such element found increment ‘distinct’ by 1.
    • Assing count[i] := distinct.
  • Return this integer array ‘count’.
Time Complexity

O(n*k^2),  Where ‘n’ is the size of the given array ‘arr’ and ‘k’ is the size of sub-array.

 

Here, we will have three nested loops, in the worst case the outermost loop runs ‘n’ times, the middle loop runs ‘k’ times and the innermost loop also runs ‘k’ times.

Space Complexity

O(n-k), where ‘n’ is the size of the given array ‘arr’ and ‘k’ is the size of sub-arrays.

 

The size of the ‘count’ vector will be of order ‘n-k’.

Code Solution
(100% EXP penalty)
Count Distinct Elements In Every Window
Full screen
Console