Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Pairs of elements problem solution

YASH PAL, 31 July 202414 February 2026
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 ' ')
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes