Given n integer numbers, count the number of ways in which we can choose two elements such
that their absolute difference is less than 32.
In a more formal way, count the number of pairs (i, j) (1 ≤ i < j ≤ n) such that |V[i] - V[j]| < 32. |X|
is the absolute value of X.
This is my code but I did n’t understand why WA? Help me to figure it out.
#include<bits/stdc++.h>
using namespace std;
int hash[10034];
int main()
{
int tc,n,A[10006],i,cnt,j;
cin>>tc;
while(tc–)
{
memset(hash,0,sizeof(hash));
cin>>n;
cnt=0;
for(i=0;i<n;i++)
{
cin>>A[i];
hash[A[i]]++;
}
for(i=2;i<=10034;i++)
{
hash[i]=hash[i]+hash[i-1];
}
for(i=0;i<n;i++)
{
cnt+=hash[A[i]+31]-hash[A[i]];
hash[A[i]]--;
}
cout<<cnt<<"\n";
}
}