Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Pairs of elements problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes