-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreedy-10-MaximumProductSubSetOfAnArray.cpp
74 lines (61 loc) · 1.44 KB
/
Greedy-10-MaximumProductSubSetOfAnArray.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
#include<bits/stdc++.h>
#define REP(i,n) for (int i = 1; i <= n; i++)
#define REP_(i,n) for (int i=0;i<=n;i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>6
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define INF 1000000000
#define endl '\n'
#define w(x) int x;cin>>x; while(x--)
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
void dfile()
{
#ifndef ONLINE_JUDGE
// For getting input from input.txt file
freopen("input.txt", "r", stdin);
// Printing the Output to output.txt file
freopen("output.txt", "w", stdout);
#endif
}
int main ()
{
dfile();
//cout<<"hello"<<endl;
int n;
cin>>n;
int a[n];
REP_(i,n) cin>>a[i];
int zero_count=0;
int negative_count=0;
int max_negative=INT_MIN;
for(int i=0;i<n;i++)
{
if(a[i]==0) zero_count++;
}
REP_(i,n) if(a[i]<0) negative_count++ max_negative=max(max_negative,a[i]);
REP_(i,n) prod=prod*a[i];
if(zero_count==n)
{
cout<<"0";
return 0;
}
else if(negative_count==1 and zero_count+negative_count==n)
{
cout<<"0";
return 0;
}
else if(negative_count&1)
{
prod=prod/max_negative;
}
cout<<prod;
return 0;
}