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

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. int main()
    4. {
    5. long long t,n,i,a;
    6. cin>>t;
    7. while(t--)
    8. {
    9. map<int,int>mp;
    10. long long ans=0;
    11. cin>>n;
    12. for(i=1; i<=n; i++)
    13. {
    14. cin>>a;
    15. ans+=mp[a-i]++;
    16. }
    17. cout<<ans<<endl;
    18. }
    19. }

    No comments

    Most View Post

    Recent post

    Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

      Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

    Powered by Blogger.