Help me in solving TREATS problem

My issue

why its is showing time limit exceeded error? how to solve it?

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--){
        long long int m,n  ,       sum=0;

        cin>>n>>m;
       long long int a[n],b[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        for(int i=0;i<n;i++){
            cin>>b[i];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if((a[i]+b[j])%m==0)
                sum++;
            }
        }
        cout<<sum<<endl; 
    }
}

Problem Link: Trick Or Treat Practice Coding Problem

Considering you are a beginner
Since the constraints on N are upto 1≤N≤2*10^5 so eventually your solution of O(N^2) would give TLE . Now seeing the constrains you can easily guess that the solution can have at max O(NlogN) complexity.
Optimised Approach :
Start by solving the GFG Problem :-https://www.geeksforgeeks.org/count-pairs-in-array-whose-sum-is-divisible-by-k/

So after solving the above problem you will realise its exactly the same problem just in our case we have been given two different arrays to choose elements from.

You can refer to my solution :-1:

#include<bits/stdc++.h>
using namespace std;
int main() {
    int t; cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        vector<int> a(n), b(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        for (int i = 0; i < n; i++) cin >> b[i];

        map<int, int> mpa, mpb;
        for (int i = 0; i < n; i++) mpa[a[i] % m]++;
        for (int i = 0; i < n; i++) mpb[b[i] % m]++;

        long long ans = 0;

        for (auto i : mpa) {
            int x = i.first;
            int r = m - x;
            if (r == m) r = 0;

            ans += (mpa[x] * 1LL) * (mpb[r] * 1LL);

        }
        cout << ans << '\n';

    }
    return 0;
}