Given an array and an integer k, find the count of distinct elements in every subarray of size k.

For example,

Input:
 
arr[] = { 2, 1, 2, 3, 2, 1, 4, 5 };
k = 5;
 
Output:
 
The count of distinct elements in subarray { 2, 1, 2, 3, 2 } is 3
The count of distinct elements in subarray { 1, 2, 3, 2, 1 } is 3
The count of distinct elements in subarray { 2, 3, 2, 1, 4 } is 4
The count of distinct elements in subarray { 3, 2, 1, 4, 5 } is 5

Practice this problem

Please note that the problem specifically targets subarrays that are contiguous (i.e., occupy consecutive positions) and inherently maintains the order of elements.

 
A naive solution is to consider every subarray in the given array and count all distinct elements in it using two nested loops, as demonstrated below in C, Java, and Python. The time complexity of this approach is O(n.k2), where n is the size of the input and k is the size of the subarray.

C


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
The count of distinct elements in subarray [0, 4] is 3
The count of distinct elements in subarray [1, 5] is 3
The count of distinct elements in subarray [2, 6] is 4
The count of distinct elements in subarray [3, 7] is 5

We know that a set doesn’t store duplicate elements. We can take advantage of this fact and insert all elements of the current subarray into a set. Then the set’s size would be the distinct element count. This reduces the time complexity to O(n.k) but uses O(k) extra space.

The algorithm can be implemented as follows in C++, Java, and Python. We can even extend the solution to print the contents of the set, as shown here.

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
The count of distinct elements in subarray [0, 4] is 3
The count of distinct elements in subarray [1, 5] is 3
The count of distinct elements in subarray [2, 6] is 4
The count of distinct elements in subarray [3, 7] is 5

We can further reduce the time complexity to O(n) by using the sliding window technique. The idea is to store the frequency of elements in the current window in a map and keep track of the distinct elements count in the current window (of size k). The code can be optimized to derive the count of elements in any window using the count of elements in the previous window by inserting the new element to the previous window from its right and removing an element from its left.

Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
The count of distinct elements in subarray [0, 2] is 2
The count of distinct elements in subarray [1, 3] is 2
The count of distinct elements in subarray [2, 4] is 3