|
| 1 | +<h2><a href="https://leetcode.com/problems/maximum-product-of-three-elements-after-one-replacement">4101. Maximum Product of Three Elements After One Replacement</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>You <strong>must</strong> replace <strong>exactly one</strong> element in the array with <strong>any</strong> integer value in the range <code>[-10<sup>5</sup>, 10<sup>5</sup>]</code> (inclusive).</p> |
| 4 | + |
| 5 | +<p>After performing this single replacement, determine the <strong>maximum possible product</strong> of <strong>any three</strong> elements at <strong>distinct indices</strong> from the modified array.</p> |
| 6 | + |
| 7 | +<p>Return an integer denoting the <strong>maximum product</strong> achievable.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<div class="example-block"> |
| 13 | +<p><strong>Input:</strong> <span class="example-io">nums = [-5,7,0]</span></p> |
| 14 | + |
| 15 | +<p><strong>Output:</strong> <span class="example-io">3500000</span></p> |
| 16 | + |
| 17 | +<p><strong>Explanation:</strong></p> |
| 18 | + |
| 19 | +<p>Replacing 0 with -10<sup>5</sup> gives the array <code>[-5, 7, -10<sup>5</sup>]</code>, which has a product <code>(-5) * 7 * (-10<sup>5</sup>) = 3500000</code>. The maximum product is 3500000.</p> |
| 20 | +</div> |
| 21 | + |
| 22 | +<p><strong class="example">Example 2:</strong></p> |
| 23 | + |
| 24 | +<div class="example-block"> |
| 25 | +<p><strong>Input:</strong> <span class="example-io">nums = [-4,-2,-1,-3]</span></p> |
| 26 | + |
| 27 | +<p><strong>Output:</strong> <span class="example-io">1200000</span></p> |
| 28 | + |
| 29 | +<p><strong>Explanation:</strong></p> |
| 30 | + |
| 31 | +<p>Two ways to achieve the maximum product include:</p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>[-4, -2, -3]</code> → replace -2 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.</li> |
| 35 | + <li><code>[-4, -1, -3]</code> → replace -1 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.</li> |
| 36 | +</ul> |
| 37 | +The maximum product is 1200000.</div> |
| 38 | + |
| 39 | +<p><strong class="example">Example 3:</strong></p> |
| 40 | + |
| 41 | +<div class="example-block"> |
| 42 | +<p><strong>Input:</strong> <span class="example-io">nums = [0,10,0]</span></p> |
| 43 | + |
| 44 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 45 | + |
| 46 | +<p><strong>Explanation:</strong></p> |
| 47 | + |
| 48 | +<p>There is no way to replace an element with another integer and not have a 0 in the array. Hence, the product of all three elements will always be 0, and the maximum product is 0.</p> |
| 49 | +</div> |
| 50 | + |
| 51 | +<p> </p> |
| 52 | +<p><strong>Constraints:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><code>3 <= nums.length <= 10<sup>5</sup></code></li> |
| 56 | + <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li> |
| 57 | +</ul> |
0 commit comments