Advertisement
maycod23

Untitled

Aug 13th, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. // maps
  6. // 1.>introduction
  7. // 2.>insertion
  8. // 3.>iteration
  9. // 4.>types of maps
  10. // 5.>complexity analysis
  11. // 6.>erasing in maps
  12. // 7.>important functions
  13. // 8.>some questions
  14.  
  15. // int n, i; cin >> n;
  16. // vector<string> v(n);
  17. // map<string, int> m;//key string value as a int
  18. // for (i = 0; i < n; i++)
  19. // {
  20. // cin >> v[i];
  21. // m[v[i]]++;
  22. // }
  23.  
  24. // int q; cin >> q;
  25. // for (i = 0; i < q; i++)
  26. // {
  27. // int x; cin >> x;
  28. //frequency of x
  29. // int count = 0;
  30. // for (int j = 0; j < v.size(); j++)
  31. // {
  32. // if (v[j] == x) count++;
  33. // }
  34. // cout << count << endl;
  35. // }
  36. // T.C=>O(N*N)
  37.  
  38.  
  39. //precomputation
  40. // int q; cin >> q;
  41. // for (i = 0; i < q; i++)
  42. // {
  43. // string x; cin >> x;
  44. // cout << "Frequency of x is " << m[x] << endl;
  45. // }
  46.  
  47.  
  48. // int n, i; cin >> n;
  49. // vector<int> v(n);
  50. // map<int, int> m;
  51. // for (i = 0; i < n; i++)
  52. // {
  53. // cin >> v[i];
  54. // //v[i] as a key in map
  55. // //frequency map
  56. // m[v[i]]++;
  57. // }
  58.  
  59. // //map size -> at any instance/time number of keys
  60. // // cout << "Map Size " << m.size() << endl;
  61.  
  62.  
  63. // // for (auto i : m)//tree
  64. // // {
  65. // // //it will print all the keys and values stored
  66. // // //corresponding
  67. // // //not linearly iterating
  68. // // //iterating with the help of iterators
  69. // // cout << i.first << " " << i.second << " " << endl;
  70. // // }
  71.  
  72. // // int q; cin >> q;
  73. // // for (i = 0; i < q; i++)
  74. // // {
  75. // // int x; cin >> x;//key
  76. // // if (m.count(x) == 0) cout << 0 << endl;
  77. // // else cout << m[x] << endl;
  78. // // }
  79.  
  80. // //1.>m.count(key)==0 ->it means that key is not present in the map ->freq=>0
  81. // //2.>if we simply we are doing m[x]->instance(node) of x
  82. // //m[something]//key creation will takes place
  83.  
  84. // //3.>m.find(key)->iterator to that key
  85. // //if(m.find(key)==m.end())//key is not present in the map
  86. // // else cout << m[key] << endl;
  87. // //4.>every key is unique in maps
  88.  
  89. // // int q; cin >> q;
  90. // // for (i = 0; i < q; i++)
  91. // // {
  92. // // int x; cin >> x;
  93. // // if (m.find(x) == m.end()) cout << 0 << endl;//->x is not present
  94. // // else cout << m[x] << endl;
  95. // // }
  96.  
  97. // // cout << "Map Size " << m.size() << endl;
  98. // //complexity
  99. // //map is a tree structure
  100. // //worstcase,best case
  101. // //search(m[x]),insertion(m[x]++;),erase
  102. // //O(log(number of keys));
  103.  
  104. // //erasing in maps
  105.  
  106. // cout << m.size() << endl;
  107. // for (auto i : m)
  108. // {
  109. // cout << i.first << " " << i.second << " " << endl;
  110. // }
  111.  
  112. // cout << endl << endl;
  113.  
  114. // int x; cin >> x;//x as a key
  115.  
  116. // if (m.find(x) != m.end())
  117. // {
  118. // m.erase(x);
  119. // }
  120.  
  121. // //m.erase->will erase he instance,key,values related to that key.
  122. // cout << m.size() << endl;
  123. // for (auto i : m)
  124. // {
  125. // cout << i.first << " " << i.second << " " << endl;
  126. // }
  127.  
  128.  
  129. int n, i; cin >> n;
  130. vector<int> v(n + 1); //1 indexing
  131. map<int, int> m;
  132. for (i = 1; i <= n; i++)
  133. {
  134. cin >> v[i];
  135. }
  136.  
  137. int ans = 0;
  138. for (int j = 1; j <= n; j++)
  139. {
  140. int temp = (v[j] - j);
  141. //to find all the i's before the j
  142.  
  143. if (m.find(temp) == m.end()) ans += 0;
  144. else ans += m[temp];
  145.  
  146. m[temp]++;
  147. }
  148.  
  149. cout << ans << endl;
  150.  
  151. //T.C=> O(nlog(number of keys));
  152. //space complexity->O(n); //optimised code
  153.  
  154.  
  155. }
  156. // You are given an array a of n integers.
  157. // Count the number of pairs of indices (i, j) such that i < j and aj−ai = j−i.
  158.  
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement