`{"id":"dcfbb50c-f58b-4a64-a18d-ac1140a2fc1e","name":"Count Distinct Elements In Every Window Of Size K","description":"1. You are given an array(arr) of integers and a number K.\r\n2. You have to find the count of distinct numbers in all windows of size k.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers\r\nA number K","outputFormat":"Space separated numbers representing the count of distinct numbers in all windows of size k.","constraints":"1 &lt;= N &lt;= 10^5\r\n1 &lt;= arr[i] &lt;= 10^4\r\n1 &lt;= k &lt;= N","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nvector<int> solution(vector<int> &arr, int k){\n // write your code here\n}\n\nint main (){\n\tint n = 0;\n\tcin >> n;\n\tvector<int> arr(n);\n\tfor(int i = 0; i < arr.size(); i++){\n\t cin >> arr[i];\n\t}\n\tint k = 0;\n\tcin >> k;\n\t\n\tvector<int> ans = solution(arr, k);\n\tfor(int a : ans){\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static ArrayList<Integer> solution(int[] arr, int k) {\r\n\t\t//write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint[] arr = new int[scn.nextInt()];\r\n\t\tfor (int i = 0; i < arr.length; i++) {\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t}\r\n\t\tint k = scn.nextInt();\r\n\t\tArrayList<Integer> ans = solution(arr,k);\r\n\t\tfor(int a : ans){\r\n\t\t\tSystem.out.print(a + \" \");\r\n\t\t}\r\n\t}\r\n\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n1 2 1 3 4 2 3 \r\n4","sampleOutput":"3 4 4 3 ","questionVideo":"https://www.youtube.com/embed/x8O9XocEioI?end=75","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"9e66c341-5b36-4ac7-8a92-d650fd523d1b","name":"Count Distinct Elements In Every Window Of Size K","slug":"count-distinct-elements-in-every-window-of-size-k","type":1}],"next":{"id":"3ab5341f-d529-4a67-8031-cc3990fc4c9a","name":"Count Distinct Elements In Every Window of Size K MCQ","type":0,"slug":"count-distinct-elements-in-every-window-of-size-k-mcq"},"prev":{"id":"83543224-84d6-4fc8-b97f-24faee60b424","name":"Check If An Array Can Be Divided Into Pairs Whose Sum Is Divisible By K","type":3,"slug":"check-if-an-array-can-be-divided-into-pairs-whose-sum-is-divisible-by-k"}}}`

# Count Distinct Elements In Every Window Of Size K

1. You are given an array(arr) of integers and a number K. 2. You have to find the count of distinct numbers in all windows of size k.

`{"id":"dcfbb50c-f58b-4a64-a18d-ac1140a2fc1e","name":"Count Distinct Elements In Every Window Of Size K","description":"1. You are given an array(arr) of integers and a number K.\r\n2. You have to find the count of distinct numbers in all windows of size k.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers\r\nA number K","outputFormat":"Space separated numbers representing the count of distinct numbers in all windows of size k.","constraints":"1 &lt;= N &lt;= 10^5\r\n1 &lt;= arr[i] &lt;= 10^4\r\n1 &lt;= k &lt;= N","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nvector<int> solution(vector<int> &arr, int k){\n // write your code here\n}\n\nint main (){\n\tint n = 0;\n\tcin >> n;\n\tvector<int> arr(n);\n\tfor(int i = 0; i < arr.size(); i++){\n\t cin >> arr[i];\n\t}\n\tint k = 0;\n\tcin >> k;\n\t\n\tvector<int> ans = solution(arr, k);\n\tfor(int a : ans){\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static ArrayList<Integer> solution(int[] arr, int k) {\r\n\t\t//write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint[] arr = new int[scn.nextInt()];\r\n\t\tfor (int i = 0; i < arr.length; i++) {\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t}\r\n\t\tint k = scn.nextInt();\r\n\t\tArrayList<Integer> ans = solution(arr,k);\r\n\t\tfor(int a : ans){\r\n\t\t\tSystem.out.print(a + \" \");\r\n\t\t}\r\n\t}\r\n\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n1 2 1 3 4 2 3 \r\n4","sampleOutput":"3 4 4 3 ","questionVideo":"https://www.youtube.com/embed/x8O9XocEioI?end=75","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"9e66c341-5b36-4ac7-8a92-d650fd523d1b","name":"Count Distinct Elements In Every Window Of Size K","slug":"count-distinct-elements-in-every-window-of-size-k","type":1}],"next":{"id":"3ab5341f-d529-4a67-8031-cc3990fc4c9a","name":"Count Distinct Elements In Every Window of Size K MCQ","type":0,"slug":"count-distinct-elements-in-every-window-of-size-k-mcq"},"prev":{"id":"83543224-84d6-4fc8-b97f-24faee60b424","name":"Check If An Array Can Be Divided Into Pairs Whose Sum Is Divisible By K","type":3,"slug":"check-if-an-array-can-be-divided-into-pairs-whose-sum-is-divisible-by-k"}}}` Editor

# Count Distinct Elements In Every Window Of Size K

easy

1. You are given an array(arr) of integers and a number K. 2. You have to find the count of distinct numbers in all windows of size k.

## Constraints

1 <= N <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= N

## Format

### Input

A number N arr1 arr2.. N numbers A number K

### Output

Space separated numbers representing the count of distinct numbers in all windows of size k.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}7 1 2 1 3 4 2 3 4```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}3 4 4 3 `

Question Video

Discussions

Show Discussion

Related Resources 