Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- // maps
- // 1.>introduction
- // 2.>insertion
- // 3.>iteration
- // 4.>types of maps
- // 5.>complexity analysis
- // 6.>erasing in maps
- // 7.>important functions
- // 8.>some questions
- // int n, i; cin >> n;
- // vector<string> v(n);
- // map<string, int> m;//key string value as a int
- // for (i = 0; i < n; i++)
- // {
- // cin >> v[i];
- // m[v[i]]++;
- // }
- // int q; cin >> q;
- // for (i = 0; i < q; i++)
- // {
- // int x; cin >> x;
- // int count = 0;
- // for (int j = 0; j < v.size(); j++)
- // {
- // if (v[j] == x) count++;
- // }
- // cout << count << endl;
- // }
- // T.C=>O(n*n)
- //precomputation
- // int q; cin >> q;
- // for (i = 0; i < q; i++)
- // {
- // string x; cin >> x;
- // cout << "Frequency of x is " << m[x] << endl;
- // }
- // int n, i; cin >> n;
- // vector<int> v(n);
- // map<int, int> m;
- // for (i = 0; i < n; i++)
- // {
- // cin >> v[i];
- // //v[i] as a key in map
- // //frequency map
- // m[v[i]]++;
- // }
- // //map size -> at any instance number of keys
- // // cout << "Map Size " << m.size() << endl;
- // // for (auto i : m)//tree
- // // {
- // // //it will print all the keys and values stored
- // // //corresponding
- // // //not linearly iterating
- // // //iterating with the help of iterators
- // // cout << i.first << " " << i.second << " " << endl;
- // // }
- // // int q; cin >> q;
- // // for (i = 0; i < q; i++)
- // // {
- // // int x; cin >> x;
- // // if (m.count(x) == 0) cout << 0 << endl;
- // // else cout << m[x] << endl;
- // // }
- // //1.>m.count(key)==0 ->it means that key is not present in the map ->freq=>0
- // //2.>if we simply we are doing m[x]->instance(node) of x
- // //m[something]//key creation will takes place
- // //3.>m.find(key)->iterator to that key
- // //if(m.find(key)==m.end())//key is not present in the map
- // // else cout << m[key] << endl;
- // //4.>every key is unique in maps
- // // int q; cin >> q;
- // // for (i = 0; i < q; i++)
- // // {
- // // int x; cin >> x;
- // // if (m.find(x) == m.end()) cout << 0 << endl;//->x is not present
- // // else cout << m[x] << endl;
- // // }
- // // cout << "Map Size " << m.size() << endl;
- // //complexity
- // //map is a tree structure
- // //worstcase,best case
- // //search(m[x]),insertion(m[x]++;),erase
- // //O(log(number of keys));
- // //erasing in maps
- // cout << m.size() << endl;
- // for (auto i : m)
- // {
- // cout << i.first << " " << i.second << " " << endl;
- // }
- // cout << endl << endl;
- // int x; cin >> x;//x as a key
- // if (m.find(x) != m.end())
- // {
- // m.erase(x);
- // }
- // //m.erase->will erase he instance,key,values related to that key.
- // cout << m.size() << endl;
- // for (auto i : m)
- // {
- // cout << i.first << " " << i.second << " " << endl;
- // }
- int n, i; cin >> n;
- vector<int> v(n + 1); //1 indexing
- map<int, int> m;
- for (i = 1; i <= n; i++)
- {
- cin >> v[i];
- }
- int ans = 0;
- for (int j = 1; j <= n; j++)
- {
- int temp = (v[j] - j);
- //to find all the i's before the j
- if (m.find(temp) == m.end()) ans += 0;
- else ans += m[temp];
- m[temp]++;
- }
- cout << ans << endl;
- //T.C=> O(nlog(number of keys));
- //space complexity->O(n); //optimised code
- }
- // You are given an array a of n integers.
- // Count the number of pairs of indices (i, j) such that i < j and aj−ai = j−i.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement