forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG_MaxProductSubarray.cpp
151 lines (119 loc) · 3.48 KB
/
GFG_MaxProductSubarray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
https://practice.geeksforgeeks.org/problems/maximum-product-subarray3604/1#
Maximum Product Subarray
https://leetcode.com/problems/maximum-product-subarray/
*/
class Solution {
public:
int maxProduct_PS(vector<int>& nums) {
int maxans=INT_MIN;
int n = nums.size();
int pref=1;
int suff=1;
for(int i=0; i<n; i++)
{
pref *= nums[i];
suff *= nums[n-1-i];
maxans = max({maxans, pref, suff});
if(pref == 0) pref =1;
if(suff == 0) suff = 1;
//cout<<"("<<pref<<","<<suff<<")";
}
return maxans;
}
int maxProduct(vector<int>& nums) {
int n = nums.size();
int maxans=nums[0];
int max_endhere = maxans; // max positive product ending at current position
int min_endhere = maxans; // min negative product ending at the current position
for(int i=1; i<n; i++)
{
/*
int mxend = nums[i]*max_endhere;
int mnendh = nums[i]*min_endhere;
max_endhere = max({ nums[i], mxend, mnendh});
min_endhere = min({ nums[i], mxend, mnendh});
maxans = max({maxans, max_endhere, min_endhere});
*/
if(nums[i]<0)
swap(max_endhere, min_endhere);
max_endhere = max( nums[i], nums[i]*max_endhere);
min_endhere = min( nums[i], nums[i]*min_endhere);
maxans = max(maxans, max_endhere);
//cout<<"("< <max_endhere<<","<<min_endhere<<")";
}
return maxans;
}
};
//GFG-----------------------------------------------------------
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
// Function to find maximum product subarray
long long maxProduct_TLE(vector<int> arr, int n) {
long long maxans=INT_MIN;
for(int i=0; i<n; i++)
{
long long prod = 1;
for(int j=i; j<n; j++)
{
prod *= arr[j];
maxans = max(maxans, prod);
}
}
return maxans;
}
//using prefix and suffix
#define ll long long
long long maxProduct_PS(vector<int> arr, int n) {
ll maxans=INT_MIN;
ll pref=1;
ll suff=1;
for(int i=0; i<n; i++)
{
pref *= arr[i];
suff *= arr[n-1-i];
maxans = max({maxans, pref, suff});
if(pref == 0) pref =1;
if(suff == 0) suff = 1;
//cout<<"("<<pref<<","<<suff<<")";
}
return maxans;
}
long long maxProduct(vector<int> arr, int n) {
ll maxans=arr[0];
ll max_endhere = arr[0]; // max positive product ending at current position
ll min_endhere = arr[0]; // min negative product ending at the current position
for(int i=1; i<n; i++)
{
ll mxend = arr[i]*max_endhere;
ll mnendh = arr[i]*min_endhere;
max_endhere = max({ (long long)arr[i], mxend, mnendh});
min_endhere = min({ (long long)arr[i], mxend, mnendh});
maxans = max({maxans, max_endhere, min_endhere});
//cout<<"("<<max_endhere<<","<<min_endhere<<")";
}
return maxans;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, i;
cin >> n;
vector<int> arr(n);
for (i = 0; i < n; i++) {
cin >> arr[i];
}
Solution ob;
auto ans = ob.maxProduct(arr, n);
cout << ans << "\n";
}
return 0;
} // } Driver Code Ends