Apartments
Key Idea: Sort both arrays, then two-pointer sweep matching each applicant to the closest apartment within tolerance k.
Solution
#include <bits/stdc++.h>
using namespace std;
//author: von_Braun
#define ll long long
#define lli long long int
#define pb push_back
#define rep(var, start, num) for(ulli var = start; var <start + num; var++)
#define all(x) x.begin(), x.end()
#define ulli unsigned long long int
#define ull unsigned long long
bool sortbysec(const pair<ll,ll> &a,const pair<ll,ll> &b) { return (a.second < b.second); }
void solve() {
int n,m,k;
cin>>n>>m>>k;
int a[n];
rep(i,0,n) {cin>>a[i];}
int b[m];
rep(i,0,m) {cin>>b[i];}
sort(a,a+n);
sort(b,b+m);
int j=0;
int i=0;
int cnt=0;
while(1) {
if (abs(a[i]-b[j]) <= k) {i++; j++; cnt++;} else {
if (a[i]>b[j]) {j++;} else {i++;}
}
if (i==n || j==m) {break;}
}
cout<<cnt<<endl;
}
int main() {
//add quotes incase input output file
//freopen(input.txt,r,stdin);
//freopen(output.txt,w,stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}