-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_DistinctEles_everyWindow.cpp
More file actions
62 lines (49 loc) · 1.12 KB
/
Copy pathCount_DistinctEles_everyWindow.cpp
File metadata and controls
62 lines (49 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int CountElements(int *arr, int n, int k)
{
int i;
int maxN = arr[0];
for(i=0; i<n; i++)
maxN = max(maxN, arr[i]);
vector<int> hashT(maxN+1, 0);
int count = 0;
vector<int> res;
for(i=0; i<k; i++)
{
if(hashT[arr[i]] == 0)
count = count+1;
hashT[arr[i]] += 1;
}
res.push_back(count);
for(i=k; i<n; i++)
{
if(hashT[arr[i-k]] == 1)
count = count-1;
hashT[arr[i-k]] -= 1;
if(hashT[arr[i]] == 0)
count = count+1;
hashT[arr[i]] += 1;
res.push_back(count);
}
for(auto it=res.begin(); it != res.end(); it++)
cout<<*it<<" ";
}
int main()
{
// int arr[] = {1,2,1,3,4,2,3};
// int n = sizeof(arr)/sizeof(arr[0]);
// int k = 4;
int n, i, k;
cout<<"\nEnter the size and k: ";
cin>>n>>k;
int arr[n];
cout<<"Enter the Array Elements: ";
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"\n";
CountElements(arr, n, k);
cout<<"\n";
return 0;
}