Find the number of distinct pairs (i,j) such that j>=i && A[i] == A[j] from an array A of N numbers.
Implementation:
Implementation:
#include <iostream>
#define MAX 1000000
using namespace std;
int main()
{
long long int T,N,A[MAX],i,j,count;
cin>>T;
while(T--)
{
count=0;
cin>>N;
for(i=0;i<N;i++)
{
cin>>A[i];
}
for(i=0;i<N;i++)
{
for(j=i;j<N;j++)
{
if(j>=i && A[i]^A[j]==0)
count++;
}
}
cout<<count<<"\n";
}
return 0;
}
You can contribute an optimized solution than the above solution using Diqus / contact us form.