CSES - Shared codeLink to this code: https://cses.fi/paste/0d6666491d791f2917703c/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
 
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
 
int n,m,k;
cin >>n>>m >>k;
std::vector<int> a(n);
for (int i = 0; i < n; ++i)
{
	cin >>a[i];
}
std::vector<int> b(m);
for (int i = 0; i <m; ++i)
{
	cin >>b[i];
}
sort(a.begin(),a.end());
sort(b.begin(), b.end());
int c=0;
int j=0;
for(int i=0;i<m && j<n;i++){
	int min=b[i]-k;
	int max=b[i]+k;
	while(a[j]<min && j<n-1){
		j++;
	}
	if(a[j]<=max && a[j]>=min){
		//cout << a[j] <<" "<< b[i] << "\n";
	 c++;j++;
	}
}
cout << c;
 
 
return 0;
	}