Header Ads Widget

T-Rex And The Pairs

 Problem:-

You are given an Array of length N. T-Rex wants you to count the number of pairs of (i, j) where 1⩽i<j⩽N such that the difference of the array elements on that indices is equal to the sum of the square of their indices.

That is, count the number of pairs of (i, j) such that it satisfies this equation (A[j] - A[i] = i2 + j2 ).

Input format 

First line contains length of the array N. (1⩽N⩽105) The second line contains N integers representating Array elements (1⩽A[i]⩽1012)

Output format

Output the number of pairs that satisfy the above condition. 
                   

Sample Input
5
4 9 6 29 30
Sample Output
3
Time Limit: 10
Memory Limit: 256
Source Limit:
Explanation

Pairs at indices : (1,2), (2,4), (1,5) satisfy the above condition.

Code:-

   in this program i used :-

ios_base::sync_with_stdio(false);
cin.tie(NULL);

this is only for reduce the time of execution we also write the code without this but when we use this this helps us to run the code faster .

#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
long long int a[n+1],i,j;
for( i=1;i<=n;i++)
{
cin>>a[i];
}
long int count=0;
long long int d,f;
for( i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
d=abs(a[j]-a[i]);
f=((i*i)+(j*j));
if(d==f)
count++;
}
}
cout<<count;
return 0;
}



Post a Comment

0 Comments