whats wrong with my code? Problem Code: XORR

#include <stdio.h>

int main(void) {
// your code goes here
int t,k;
scanf("%d",&t);
for(k=0;k<t;k++)
{
long long int i,j,n,a[20000],b[20000],p=0;
scanf("%ld",&n);
for(i=0;i<n;i++)
scanf("%ld",&a[i]);
for(i=0;i<n;i++)
scanf("%ld",&b[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((a[i]^a[j])==(b[i]^b[j]))
p++;
}
}
printf("%ld\n",p);
}
return 0;
}

The array size in the declatation

is not sufficient to store all the array elements when N is 3\times10^5. On the other hand, the double nested loop

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((a[i]^a[j])==(b[i]^b[j]))
p++;
}
}

is surely going to get TLE verdict when N is large. If you are using C++, you can use the STL map container to compute the answer. Otherwise, you should implement your own map data structure in C.

Check the following solution for AVL tree based solution in C.

Accepted

1 Like