CSES Solutions
#3219Sliding Window Problems

Sliding Window Mex

View on CSES

Solution

3219-Sliding_Window_Mex.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  set<int> s;
9  for(int i=0;i<=k;++i) s.emplace(i);
10  int a[n]; for(int i=0;i<n;++i){
11    cin>>a[i];
12    if(++mp[a[i]]==1) s.erase(a[i]);
13    if(i>=k) if(--mp[a[i-k]]==0) s.emplace(a[i-k]);
14    if(i>=k-1){
15      cout<<*s.begin()<<" ";
16    }
17  }
18  cout<<'\n';
19  return 0;
20}

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