Help me in solving TREATS problem

My issue

it failed to compile for a hidden test case
what do i need to change

My code

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

int main() {
int t;
cin>>t;
while(t--){
    int n,m;
    cin>>n>>m;
    int a[n];
    int b[n];

    for(int i=0;i<n;i++){
        cin>>a[i];
        cin>>b[i];}
        if(m==1){
            cout<<n*n<<endl;
        }
        else{
          int sum=0;

        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

you will get TLE as you are solving the problem in O(N^2)
you can use map or array to store frequency of A[i]%m
int n;cin>>n;
int m;cin>>m;
map<int,int>ma;
for(int i=0;i<n;i++){
int x;cin>>x;
ma[x%m]++;
}
int ans=0;
for(int i=0;i<n;i++){
int x;cin>>x;
x%=m;
int rem = (m-x)%m;
ans+= ma[rem%m];
}
cout<<ans<<endl;

1 Like

thank you sm!!