In this HackerEarth Pairs of elements problem solution, You are given an array of length N. You are required to count the number of (i,j) pairs 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 the count of the number of pairs of (i, j) such that it satisfies this equation (A[j] - A[i] = i*i + J*J).


HackerEarth Pairs of elements problem solution


HackerEarth Pairs of elements problem solution.

#include<bits/stdc++.h>
#include<climits>

using namespace std;

int main() {
  cin.tie(0);
  long long int n;
  cin >> n;
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  unordered_map<long long int, long long int> mp1;
  for (int i = 0; i < n; i++) {
    mp1[(a[i] + (long long)((long long int)(i + 1) * (i + 1)))]++;
  }
  unordered_map<long long int, long long int> mp2;
  for (int i = 0; i < n; i++) {
    mp2[(a[i] - (long long)((long long int)(i + 1) * (i + 1)))]++;
  }

  long long int cnt = 0;
  for (auto it : mp1) {
    cnt += (mp2[it.first]*it.second);
  }
  cout << cnt << endl;
}

Second solution

n = 100
X = int(1e11)
print(n)
for i in range(n // 2):
    print(X - (i + 1) ** 2, end=' ')
for i in range(n // 2, n):
    print(X + (i + 1) ** 2, end='\n' if i == n - 1 else ' ')