CSES Solutions
#3222Sliding Window Problems

Sliding Window Distinct Values

View on CSES

Solution

3222-Sliding_Window_Distinct_Values.cpp
1#include<bits/stdc++.h>
2using namespace std;
3#define int long long
4signed main(){
5  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
6  int n,k; cin>>n>>k;
7  map<int,int> mp;
8  int a[n]; for(int i=0,res=0;i<n;++i){
9    cin>>a[i];
10    if(++mp[a[i]]==1) ++res;
11    if(i>=k) if(!--mp[a[i-k]]) --res;
12    if(i>=k-1) cout<<res<<" ";
13  }
14  cout<<'\n';
15  return 0;
16}

Editorial not yet generated for this problem. Run the editorial generation script to add hints and detailed explanations.