Maximum Product of Three Elements After One Replacement
You are given an integer array nums.
You must replace exactly one element in the array with any integer value in the range [-10^5, 10^5] inclusive.
After performing this single replacement, determine the maximum possible product of any three elements at distinct indices from the modified array.
Return an integer denoting the maximum product achievable.
Example 1
Input
nums = [-5,7,0]Output
3500000Replacing
0 with -10^5 gives [-5, 7, -10^5], whose product is (-5) * 7 * (-10^5) = 3500000, which is maximum.Example 2
Input
nums = [-4,-2,-1,-3]Output
1200000Replacing either
-2 or -1 with 10^5 can produce a maximum product of 1200000 using two remaining negative values.Constraints
- 3 <= nums.length <= 10^5
- -10^5 <= nums[i] <= 10^5