-
Notifications
You must be signed in to change notification settings - Fork 25
/
MaxProductOfThree.cpp
47 lines (39 loc) · 1.33 KB
/
MaxProductOfThree.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
// https://app.codility.com/programmers/lessons/6-sorting/max_product_of_three/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected time complexity: O(N*log(N))
//
//
#include <algorithm>
#include <vector>
using namespace std;
// Be careful of int overflow, do math in long long int space
// Keep in mind that two negatives will create a positive
// Greatest will always be: (X*Z > Y*Z ? X*Z : Y*Z) where
// There are at least 5 elements
// X = biggest two negative numbers (or first two elements of array)
// Z = largest number (last element of array)
// Y = second and third largest number (N-1 and N-2, respectively)
// Edge cases:
// 3 elements - just return A[P]*A[Q]*A[R]
// 4 elements - [a b c d] - return greater of abd or bcd
int solution(vector<int> &A) {
int N = A.size();
// Sort the array using built in sort, this is O(N*log(N))
std::sort(A.begin(), A.end());
if ( N == 3 ) {
return (A[0] * A[1] * A[2]);
}
if ( N == 4 ) {
if ( A[2] == 0 ) return A[0] * A[1] * A[2];
long long int abd = A[0] * A[1] * A[3];
long long int bcd = A[1] * A[2] * A[3];
return ( (abd>bcd) ? abd : bcd );
}
long long int X = A[0] * A[1];
long long int Y = A[N-3] * A[N-2];
long long int Z = A[N-1];
return ( ((X*Z)>(Y*Z)) ? (X*Z) : (Y*Z) );
}