3732. Maximum Product of Three Elements After One Replacement Medium
1/**
2 * [3732] Maximum Product of Three Elements After One Replacement
3 *
4 * You are given an integer array nums.
5 * You must replace exactly one element in the array with any integer value in the range [-10^5, 10^5] (inclusive).
6 * After performing this single replacement, determine the maximum possible product of any three elements at distinct indices from the modified array.
7 * Return an integer denoting the maximum product achievable.
8 *
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [-5,7,0]</span>
12 * Output: <span class="example-io">3500000</span>
13 * Explanation:
14 * Replacing 0 with -10^5 gives the array [-5, 7, -10^5], which has a product (-5) * 7 * (-10^5) = 3500000. The maximum product is 3500000.
15 * </div>
16 * <strong class="example">Example 2:
17 * <div class="example-block">
18 * Input: <span class="example-io">nums = [-4,-2,-1,-3]</span>
19 * Output: <span class="example-io">1200000</span>
20 * Explanation:
21 * Two ways to achieve the maximum product include:
22 *
23 * [-4, -2, -3] → replace -2 with 10^5 → product = (-4) * 10^5 * (-3) = 1200000.
24 * [-4, -1, -3] → replace -1 with 10^5 → product = (-4) * 10^5 * (-3) = 1200000.
25 * The maximum product is 1200000.</div>
26 * <strong class="example">Example 3:
27 * <div class="example-block">
28 * Input: <span class="example-io">nums = [0,10,0]</span>
29 * Output: <span class="example-io">0</span>
30 * Explanation:
31 * 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.
32 * </div>
33 *
34 * Constraints:
35 *
36 * 3 <= nums.length <= 10^5
37 * -10^5 <= nums[i] <= 10^5
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/maximum-product-of-three-elements-after-one-replacement/
43// discuss: https://leetcode.com/problems/maximum-product-of-three-elements-after-one-replacement/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn max_product(nums: Vec<i32>) -> i64 {
49
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_3732() {
61 }
62}
63Back
© 2026 bowen.ge All Rights Reserved.