New update is available. Click here to update.

# Count Distinct Element in Every K Size Window

Last Updated: 1 Dec, 2020
Difficulty: Easy

## PROBLEM STATEMENT

#### You are given an array ‘ARR’ of size ‘N’ and an integer ‘K’. Your task is to find the total number of distinct elements present in every ‘K’ sized window of the array. A ‘K’ sized window can also be viewed as a series of continuous ‘K’ elements present in the sequence.

##### Note:
``````1. The size of ‘ARR’ will always be greater than or equal to the ‘K’.
2. Here window refers to a subarray of ‘ARR’. Hence ‘K’ sized window means a subarray of size ‘K’.
3. You are not required to print the output explicitly. It has already been taken care of. Just implement the function and return an array of the count of all distinct elements in the ‘K’ size window.
``````

#### Example

``````Consider ARR = [ 1, 2, 1, 3, 4, 2, 3 ] and K = 3.
`````` ``````As per the given input, we have a sequence of numbers of length 7, and we need to find the number of distinct elements present in all the windows of size 3.

Window-1 has three elements { 1, 2, 1 } and only two elements { 1, 2 } are distinct because 1 is repeating two times.
Window-2 has three elements { 2, 1, 3 } and all three elements are distinct { 2, 1, 3 }.
Window-3 has three elements { 1, 3, 4 } and all three elements are distinct { 1, 3, 4 }.
Window-4 has three elements { 3, 4, 2 } and all three elements are distinct { 3, 4, 2 }.
Window-5 has three elements { 4, 2, 3 } and all three elements are distinct { 4, 2, 3 }.

Hence, the count of distinct elements in all K sized windows is { 2, 3, 3, 3, 3 }.
``````
##### Input format:
``````The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and ‘K’, denoting the number of elements in the array and the size of the window.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'ARR'.
``````
##### Output Format:
``````For each test case, print an array that contains the number of distinct elements in all ‘K’ size windows, and the count of distinct elements must be from the left to the right window.

Print the output of each test case in a separate line.
``````
##### Constraints:
``````1 <= T <= 10
1 <= N <= 10 ^ 5
1 <= K <= N
1 <=ARR[i] <= 10 ^ 9

Where 'T' denotes the number of test cases, 'N' denotes the number of elements in the array, ‘K’ denotes the size of the window, and 'ARR[i]' denotes the 'i-th' element of the array 'ARR'.

Time limit: 1 second
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.8350898,"math_prob":0.9885632,"size":2218,"snap":"2023-14-2023-23","text_gpt3_token_len":631,"char_repetition_ratio":0.17795844,"word_repetition_ratio":0.077097505,"special_character_ratio":0.30072138,"punctuation_ratio":0.13374485,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9959286,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,7,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-25T10:16:49Z\",\"WARC-Record-ID\":\"<urn:uuid:c94ada03-fb1f-4b76-a369-38f09b6f37da>\",\"Content-Length\":\"158094\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d4097775-55e7-43c7-9528-203a8d938c2a>\",\"WARC-Concurrent-To\":\"<urn:uuid:8125d84f-086b-4f72-856a-83c240280aba>\",\"WARC-IP-Address\":\"18.67.65.29\",\"WARC-Target-URI\":\"https://www.codingninjas.com/codestudio/problem-details/count-distinct-element-in-every-k-size-window_920336\",\"WARC-Payload-Digest\":\"sha1:OCKVWWMRZ2HOTDREX3G32PFSV3NIFYCY\",\"WARC-Block-Digest\":\"sha1:WRFUELKR2WJX557FBIODAPLYVBNW2JYG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945323.37_warc_CC-MAIN-20230325095252-20230325125252-00223.warc.gz\"}"}