fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n;
  7. cin >> n;
  8.  
  9. long long a[n];
  10.  
  11. for(int i = 0; i < n; i++)
  12. {
  13. // Index: 0 1 2 3
  14. // Array: 2 3 -2 4
  15. cin >> a[i];
  16. }
  17.  
  18. vector<long long> prefix(n, 0);
  19. vector<long long> suffix(n, 0);
  20.  
  21. long long currentelement = 1;
  22. long long ans = LLONG_MIN;
  23.  
  24. // Prefix: Left to Right
  25. for(int i = 0; i < n; i++)
  26. {
  27. currentelement = currentelement * a[i];
  28.  
  29. prefix[i] = currentelement;
  30.  
  31. // Update maximum product
  32. ans = max(ans, currentelement);
  33.  
  34. // If current element is 0, reset product to 1
  35. if(currentelement == 0)
  36. {
  37. currentelement = 1;
  38. }
  39. }
  40.  
  41. currentelement = 1;
  42.  
  43. // Suffix: Right to Left
  44. for(int i = n - 1; i >= 0; i--)
  45. {
  46. currentelement = currentelement * a[i];
  47.  
  48. suffix[i] = currentelement;
  49.  
  50. // Update maximum product
  51. ans = max(ans, currentelement);
  52.  
  53. // If current element is 0, reset product to 1
  54. if(currentelement == 0)
  55. {
  56. currentelement = 1;
  57. }
  58. }
  59.  
  60. cout << "Maximum product: " << ans << endl;
  61.  
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0s 5344KB
stdin
Standard input is empty
stdout
Maximum product: 9179285098636247040