CSES Solutions
#1084Sorting and Searching

Apartments

View on CSES

Solution

1084-Apartments.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,m,k; cin>>n>>m>>k;
7  int a[n]; for(auto&x:a) cin>>x;
8  int b[m]; for(auto&x:b) cin>>x;
9  sort(a,a+n); sort(b,b+m);
10  int ans=0,i=0;
11  for(auto&x:a){
12    while(i<m&&b[i]+k<x) ++i;
13    if(i<m&&x-k<=b[i]&&b[i]<=x+k) ++ans,++i;
14  }
15  cout<<ans<<'\n';
16  return 0;
17}

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