-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximumProduct.js
More file actions
33 lines (31 loc) · 983 Bytes
/
MaximumProduct.js
File metadata and controls
33 lines (31 loc) · 983 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
// Find maximum product of two integers in an array
function getMaxProduct(arr) {
if (!arr.length) return null;
let i = 0;
let max1 = Number.MIN_SAFE_INTEGER, max2 = Number.MIN_SAFE_INTEGER;
let min1 = Number.MAX_SAFE_INTEGER, min2 = Number.MAX_SAFE_INTEGER;
console.log(min1, min2, max1, max2);
while(i < arr.length) {
switch(true) {
case arr[i] > max1:
max2 = max1;
max1 = arr[i];
break;
case arr[i] > max2:
max2 = arr[i];
break;
case arr[i] < min1:
min2 = min1;
min1 = arr[i];
break;
case arr[i] > min2:
min2 = arr[i];
break;
}
console.log(arr[i],min1, min2, max1, max2);
i++;
}
console.log(min1, min2, max1, max2);
return Math.max(min1*min2, max1*max2);
}
console.log(getMaxProduct([-10,-3,5,6,-2]));