0
How to find the max product formed by multiplying three numbers of an un sorted array?
We can not sort the array. Input- {2,5,-2,6,-3,8,0,-7,-9,4}
2 ответов
0
Parse through to get the 3 biggest numbers and multiply. The harder question is if you want to get the max from 3 consecutive numbers.
0
Two possibilities:
* max product of only positive numbers
* max product of two negative and one positive number
You can search for the two highest numbers and for the two minimal numbers
8 * 6 = 48
(-7) * (-9) = 63
You don't need to search the third positive value because you know it can not be larger than 8.
8 * 6 * (n < 8) can not be larger than (-7) * (-9) * 8
Here is a code but I didn't test all cases (and I am sure it can be optimized):
https://code.sololearn.com/cG7OiVJuTWVK/?ref=app