Saturday, December 23, 2023

[Hackerearth] Find Pairs

Problem Link    : Find Pairs 
Category        : Math
Contest         : December Circuits'23        
#include "bits/stdc++.h"
using namespace std;
#define int     long long int
#define endl    '\n'
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(12);
 
    bool FILEIO = 1;
    if (FILEIO and fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
    }
 
    int n;
    cin >> n;
    map<int, int> cache;
    for (int i = 1; i <= n; i++) {
        int num;
        cin >> num;
        cache[num - i]++;
    }
    int ans = 0;
    for (auto it : cache) {
        ans += it.second * (it.second - 1);
    }
    cout << ans << endl; 

} 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.