Link to this code: https://cses.fi/paste/69fd1810b6927c73164517/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define loop(i,j,n) for(ll i=j;i<n;i++)
int main()
{
    SPEED;
        ll n,m,k;
        cin>>n>>m>>k;
        ll arr1[n],arr2[m];
        loop(i,0,n)
        {
            cin>>arr1[i];
        }
        loop(i,0,m)
        {
            cin>>arr2[i];
        }
        sort(arr1,arr1+n);
        sort(arr2,arr2+n);
        int counter=0;
        int i=0,j=0;
        while(i<n&&j<m)
        {
            if(arr1[i]>=arr2[j]-k&&arr1[i]<=arr2[j]+k)
            {
                counter++;
                i++;
                j++;
            }else if(arr1[i]>=arr2[j]+k)
            {
                j++;
            }else{
                i++;
            }
        }
        cout<<counter;
}