[Competitive programming] How does one guarantee that i < j for an array of size n while counting? Completely confused about a solution.
Problem : https://codeforces.com/contest/1520/problem/D
Solution:
#include <bits/stdc++.h>
using namespace std;
unsigned long long getCount(vector< long long int> vec){
long long count = 0;
unordered_map< long long, long long> hash_i;
for ( long long i = 0; i < vec.size(); i++){
hash_i[vec[i] - i]++; // Count how many times a_i - i occurs
}
for (auto i = hash_i.begin(); i != hash_i.end(); i++){
count += (hash_i[i->first] * (hash_i[i->first] - 1))/2; // What exactly is this counting? How does this guarantee i < j when counting? Also shouldn't for each index the count be 1 less than the count we got at that index? Completely confused about this.
}
return count;
}
int main() {
unsigned long long t;
cin>>t;
while (t--){
unsigned long long n;
cin>>n;
vector< long long> vec;
while (n--){
long long x;
cin>>x;
vec.push_back(x);
}
cout<<getCount(vec)<<endl;
}
return 0;
}