Advertisement
maycod23

maps_prefix_array.cpp

Mar 8th, 2022
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     // 1. > maps in sorted order (revise)
  6.     // 2. > unordered maps (hashing)
  7.     // 3. > when to use maps and when to use unordered maps
  8.  
  9.  
  10.     // 4. > concept of prefix array(v.v.v.imp)
  11.     // 5. > check whether the array has subarray with 0 sum
  12.     // 6. > max length of the subarray with 0 sum
  13.     // 7. > count of subarrays with 0 sum
  14.     // 8. > check whether the array has subarray with sum k
  15.     // 9. > max length of the subarray with sum k
  16.     // 10. > count of subarrays with sum k
  17.  
  18.  
  19.     // map<int, int> m;
  20.     // m[2]++; m[3]++;
  21.     // for (auto i : m)
  22.     // {
  23.     //  cout << i.first << " " << i.second << endl;
  24.     // }
  25.  
  26.     // m[1]++;
  27.     // for (auto i : m)
  28.     // {
  29.     //  cout << i.first << " " << i.second << endl;
  30.     // }
  31.  
  32.     // unordered_map<int, int> um;
  33.  
  34.  
  35.     // um[3]++; um[2]++;
  36.     // for (auto i : um)
  37.     // {
  38.     //  cout << i.first << " " << i.second << endl;
  39.     // }
  40.  
  41.     // um[1]++;
  42.     // for (auto i : um)
  43.     // {
  44.     //  cout << i.first << " " << i.second << endl;
  45.     // }
  46. //internally implemented in the form of hash table
  47. //no order of keys
  48.  
  49.  
  50.  
  51.     //prefix array
  52.     // int n; cin >> n;
  53.     // vector<int> v(n + 1);
  54.     // for (int i = 1; i <= n; i++) cin >> v[i];
  55.     // int q; cin >> q;
  56.     // while (q--)
  57.     // {
  58.     //  int l, r; cin >> l >> r;
  59.     //  int sum = 0;
  60.     //  for (int i = l; i <= r; i++) sum += v[i];
  61.     //  cout << sum << endl;
  62.     // }
  63.  
  64.     //worst case complexity=>O(N*N)
  65.     //1s allowed limit=>10^8
  66.  
  67.  
  68.     int n; cin >> n;
  69.     vector<int> v(n + 1);//1 indexing
  70.     for (int i = 1; i <= n; i++) cin >> v[i];
  71.     v[0] = 0;
  72.     // vector<int> prefix(n + 1, 0);
  73.     // for (int i = 1; i <= n; i++)
  74.     // {
  75.     //  prefix[i] += prefix[i - 1] + v[i];//formula for prefix array
  76.     // }
  77.  
  78.     // // for (int i = 1; i <= n; i++) cout << v[i] << " "; cout << endl;
  79.     // // for (int i = 1; i <= n; i++) cout << prefix[i] << " "; cout << endl;
  80.  
  81.  
  82.     // //use of prefx array
  83.     // int q; cin >> q;
  84.     // while (q--)
  85.     // {
  86.     //  int l, r; cin >> l >> r;
  87.     //  int sum = prefix[r] - prefix[l - 1]; //2nd formlua
  88.     //  // O(1)
  89.     //  cout << sum << endl;
  90.     // }
  91.  
  92.     // O(N)->optimisation using prefix array
  93.  
  94. //check whether the array has subarray with 0 sum
  95.  
  96.     map<int, int> m;
  97.     int tempsum = 0; bool flag = false;
  98.     for (int i = 1; i <= n; i++)
  99.     {
  100.         tempsum += v[i];
  101.         if ((tempsum == 0) || (m.find(tempsum) != m.end()))
  102.         {
  103.             //either sum is zero or it is already present
  104.             flag = true; break;
  105.         }
  106.         else
  107.         {
  108.             m[tempsum]++;
  109.         }
  110.     }
  111.     if (flag) cout << "FOUND" << endl;
  112.     else cout << "NOT FOUND" << endl;
  113.  
  114.     // T.C->O(n*logn)
  115.     // S.C->O(n)
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement