-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoOfsubarrWithKDistInte.cpp
More file actions
executable file
·50 lines (43 loc) · 1.09 KB
/
Copy pathnoOfsubarrWithKDistInte.cpp
File metadata and controls
executable file
·50 lines (43 loc) · 1.09 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
#include<bits/stdc++.h>
using namespace std;
int subarraysWithKDistinct(vector<int>& s, int K) {
map<int,int> count;
int n = s.size();
if(n<=1){
return n;
}
int lo=0;
int ans=0;
int curr_len=0;
for(int hi=0;hi<n;hi++){
count[s[hi]]++;
while(count.size()>K && count.size()){
if(lo<=hi && count[s[lo]]>0)
count[s[lo]]--;
if(count[s[lo]] ==0){
count.erase(s[lo]);
}
if(count.size()==K){
cout<<"lo:"<<lo<<" hi:"<<hi<<endl;
ans++;
}
lo++;
}
if(count.size()==K){
cout<<"lo:"<<lo<<" hi:"<<hi<<endl;
ans++;
}
}
return ans;
}
int main(){
int n,k;
cin>>n>>k;
vector<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
int ans = subarraysWithKDistinct(a,k);
cout<<"ANS:"<<ans<<endl;
return 0;
}