Codeforces 1520D. Same Differences Solution
Explain: Given Aj - Ai = j-i
we get Aj-j=Ai-i
So, we want count how many difference are same .
example: index :1 2 3 4 5 6
array : 1 6 3 4 5 6
index- array value 0 4 0 0 0 0
so the ans is how many time count the same value repeat.
Solution in C++:
///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- long long t,n,i,a;
- cin>>t;
- while(t--)
- {
- map<int,int>mp;
- long long ans=0;
- cin>>n;
- for(i=1; i<=n; i++)
- {
- cin>>a;
- ans+=mp[a-i]++;
- }
- cout<<ans<<endl;
- }
- }
No comments