CSES - Shared codeLink to this code: https://cses.fi/paste/68f0acfb5df3e858176be6/
#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){
		j++;
	}
	if(a[j]<=max && a[j]>=min){c++;j++;}
}
cout << c;
 
 
return 0;
	
}