-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum of count.cpp
58 lines (57 loc) · 887 Bytes
/
sum of count.cpp
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
/*
given an array and a window size that is sliding along the array, find the sum of the count of unique elements in each window
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int arr[20];
int soln[20]={0};
int count=0,n;
int sum=0;
int win_size;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cin>>win_size;
for(int i=0;i<win_size;i++)
{
soln[arr[i]]++;
if(soln[arr[i]]==1)
{
count++;
}
if(soln[arr[i]]==2){
count--;
}
}
sum+=count;
cout<<count<<endl;
for(int i=win_size;i<n;i++){
soln[arr[i-win_size]]--;
soln[arr[i]]++;
if(soln[arr[i-win_size]]==0)
{
count--;
}if(soln[arr[i-win_size]]==1)
{
count++;
}
if(soln[arr[i]]==1)
{
//soln[arr[i]]++;
count++;
}
else if(soln[arr[i]]==2)
{
count--;
}
cout<<count<<endl;
sum+=count;
}
cout<<endl;
cout<<sum;
return 0;
}