-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighestProduct.cpp
More file actions
56 lines (37 loc) · 822 Bytes
/
HighestProduct.cpp
File metadata and controls
56 lines (37 loc) · 822 Bytes
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
/*
Highest Product
Asked in:
Coursera
Amazon
Given an array A, of N integers A.
Return the highest product possible by multiplying 3 numbers from the array.
NOTE: Solution will fit in a 32-bit signed integer.
Input Format:
The first and the only argument is an integer array A.
Output Format:
Return the highest possible product.
Constraints:
1 <= N <= 5e5
Example:
Input 1:
A = [1, 2, 3, 4]
Output 1:
24
Explanation 1:
2 * 3 * 4 = 24
Input 2:
A = [0, -1, 3, 100, 70, 50]
Output 2:
350000
Explanation 2:
70 * 50 * 100 = 350000
Seen thi
*/
int Solution::maxp3(vector<int> &A) {
sort(A.begin(),A.end());
if(A.size()==1)
return A[A.size()-1];
if(A.size()==2)
return A[A.size()-1]*A[A.size()-2];
return max(A[A.size()-1]*A[A.size()-2]*A[A.size()-3],A[0]*A[1]*A[A.size()-1]);
}