Advertisement
i_love_rao_khushboo

Untitled

Sep 17th, 2022
1,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. // Ref: https://www.techiedelight.com/find-maximum-product-subarray-given-array/
  2. //      https://www.youtube.com/watch?v=hJ_Uy2DzE08
  3. /******************************************************************************************************/
  4.  
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8. #define ll long long
  9. #define ull unsigned long long
  10. #define pb push_back
  11. #define mp make_pair
  12. #define F first
  13. #define S second
  14. #define PI 3.1415926535897932384626
  15. #define deb(x) cout << #x << "=" << x << endl
  16. #define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
  17. typedef pair<int, int> pii;
  18. typedef pair<ll, ll> pll;
  19. typedef vector<int> vi;
  20. typedef vector<ll> vll;
  21. typedef vector<ull> vull;
  22. typedef vector<pii> vpii;
  23. typedef vector<pll> vpll;
  24. typedef vector<vi> vvi;
  25. typedef vector<vll> vvll;
  26. typedef vector<vull> vvull;
  27. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  28. int rng(int lim) {
  29.     uniform_int_distribution<int> uid(0,lim-1);
  30.     return uid(rang);
  31. }
  32.  
  33. const int INF = 0x3f3f3f3f;
  34. const int mod = 1e9+7;
  35.  
  36. // Function to return the maximum product of a subarray of a given array
  37. ll max_prod_subarr(vll &v) {
  38.     int n = (int)v.size();
  39.     if(n == 0) return 0;
  40.    
  41.     // to store the final result
  42.     ll res = v[0];
  43.    
  44.     // max_ending_here = maximum product ending at the current index.
  45.     // min_ending_here = minimum product ending at the current index.
  46.     ll max_ending_here = v[0];
  47.     ll min_ending_here = v[0];
  48.    
  49.     for(int i = 1; i < n; i++) {
  50.         ll tmp = max_ending_here;
  51.        
  52.         // update the maximum product ending at the current index
  53.         max_ending_here = max(v[i], max(v[i] * max_ending_here, v[i] * min_ending_here));
  54.        
  55.         // update the minimum product ending at the current index
  56.         min_ending_here = min(v[i], min(v[i] * min_ending_here, v[i] * tmp));
  57.        
  58.         // update final result
  59.         res = max(res, max_ending_here);
  60.     }
  61.    
  62.     return res;
  63. }
  64.  
  65. void solve()
  66. {
  67.     int n; cin >> n;
  68.     vll v(n);
  69.     for(int i = 0; i < n; i++) cin >> v[i];
  70.    
  71.     cout << max_prod_subarr(v) << "\n";
  72. }
  73.  
  74. int main()
  75. {
  76.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  77.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  78.  
  79.     // #ifndef ONLINE_JUDGE
  80.     //     freopen("input.txt", "r", stdin);
  81.     //     freopen("output.txt", "w", stdout);
  82.     // #endif
  83.  
  84.     int t = 1;
  85.     // int test = 1;
  86.     // cin >> t;
  87.     while(t--) {
  88.       // cout << "Case #" << test++ << ": ";
  89.       solve();
  90.     }
  91.  
  92.     return 0;
  93. }
  94.  
  95. // Time complexity: O(n)
  96. // Space complexity: O(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement