Pairs of elements

Problem:-

You are given an array of length 

N

. You are required to count the number of 

(i,j)

 pairs where 

1i<jN

 such that the difference of the array elements on that indices is equal to the sum of the square of their indices.

That is the count of the number of pairs of 

(i,j)

 such that it satisfies this equation (

A[j]A[i]=i2+j2

).

Input format 

  • The first line contains the length of the array 
    N

    . (

    1N105

    )

  • The second line contains 
    N

     integers representing array elements. (

    1A[i]1012

    )

Output format

Print the number of pairs that satisfy the provided 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:- 

  You can submit this solution is in c++,c++14 and c++17

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



Leave a Reply

Your email address will not be published.