CSES Solutions
#3224Sliding Window Problems

Sliding Window Mode

View on CSES

Solution

3224-Sliding_Window_Mode.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  priority_queue<pair<int,int>>pq;
9  int a[n]; for(int i=0;i<n;++i){
10    cin>>a[i];
11    pq.emplace(++mp[a[i]],-a[i]);
12    if(i>=k) --mp[a[i-k]];
13    if(i>=k-1){
14      while(mp[-pq.top().second]<pq.top().first) pq.pop();
15      cout<<-pq.top().second<<" ";
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.