for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
d={}
for i in range(n):
for j in range(i+1,n):
d[l[i]^l[j]]=d.get(l[i]^l[j],0)+1
ans=0
for k,v in d.items():
ans+=v*(v-1)*4
print(ans)
For this code why TLE at test case 9 but where as the same logic in c++ is accepted
My code
# cook your dish here
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
d={}
for i in range(n):
for j in range(i+1,n):
d[l[i]^l[j]]=d.get(l[i]^l[j],0)+1
ans=0
for k,v in d.items():
ans+=v*(v-1)*4
print(ans)
include
#include<bits/stdc++.h> include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision; define ll int128_t
// define int long long define endl “\n” define mod 1000000007
using namespace std;
int power(int a, int b) {
int res = 1;
while (b) {
if (b & 1) {
res = res * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return res;
}
int inv(int a) {
return power(a, mod - 2);
}
int32_t main() {
cin.tie(0)->sync_with_stdio(false);
int t;cin>>t;
while(t–){
int n;cin>>n;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
long long ans=0;
vectormp(2000000,0);
for(int i=0;i<n;i++){
// for(int j=i+1;j<n;j++){
// ans+=mp[a[i]^a[j]];
// }
for(int k=0;k<i;k++){
ans+=mp[a[i]^a[k]];
mp[a[i]^a[k]]++;
}
}