Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,375 views

Given an array of integers :

Find the count of pairs whose {A[i] - A[j] = j^2 - i^2}, where 0 <= i < j <= n - 1

Input Format

Integer "N"

N space separated integers ---example--->

arr = 4 9 8 1 7 3 6 10 5 2

Link to problem for submitting : https://www.hackerrank.com/contests/desiqna-oa-practice-contest-00/challenges/a-325

in Interview-Experiences by Expert (108,170 points)
edited by | 1,375 views

1 Answer

0 like 0 dislike
Best answer
C++ code solution : 



#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll ;  

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    
    ll n;
    cin>>n ; 
    ll a[n+1]={0};
    ll v = 0 ;
    while(v<=n-1){
        cin>>a[v] ; 
        v++;
    }
    
    ll kk=0;
    unordered_map <ll,ll> b ; 
    ll i = 0 ; 
    while(i<=n-1){
        ll gg = a[i] + i*i ; 
        kk += (b[gg]);
        b[gg]++;
        i++;
    }
    cout<<kk;
    return 0;
}

 

by Expert (108,170 points)
selected by