Advertisement
maycod23

maps_practise

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