Wrong answer for the given problem

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";
}

}

hello Tirth Bal

please provide problem link so that i could think about it

have you tried with long ? int may overflow

can element of array be negative ?

if max is largest element of array then size of hash array must be >= max+31. and one thing that i notice is that you did not sort array ?

if larger element come first then you will lose the value of hash array of that element (hash[i]–) which will be used when smaller element come later. and if you don’t wanna sort then make two copy of hash array hash1,hash2

ans+=hash1[A[i]+31]-hash2[A[i]];

hash2[A[i]]–;

i think it would help you. although i don’t know the Constraints of question may be it is wrong :frowning:
please specify if any :-

1 Like
//