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
Inputnums = [-5,7,0]
Output3500000
Replacing 0 with -10^5 gives [-5, 7, -10^5], whose product is (-5) * 7 * (-10^5) = 3500000, which is maximum.
Example 2
Inputnums = [-4,-2,-1,-3]
Output1200000
Replacing 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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate