Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums.
You must replace exactly one element in the array with any integer value in the range [-105, 105] (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: 3500000
Explanation:
Replacing 0 with -105 gives the array [-5, 7, -105], which has a product (-5) * 7 * (-105) = 3500000. The maximum product is 3500000.
Example 2:
Input: nums = [-4,-2,-1,-3]
Output: 1200000
Explanation:
Two ways to achieve the maximum product include:
[-4, -2, -3] → replace -2 with 105 → product = (-4) * 105 * (-3) = 1200000.[-4, -1, -3] → replace -1 with 105 → product = (-4) * 105 * (-3) = 1200000.Example 3:
Input: nums = [0,10,0]
Output: 0
Explanation:
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.
Constraints:
3 <= nums.length <= 105-105 <= nums[i] <= 105Problem summary: You are given an integer array nums. You must replace exactly one element in the array with any integer value in the range [-105, 105] (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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Greedy
[-5,7,0]
[-4,-2,-1,-3]
[0,10,0]
maximum-product-of-three-numbers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3732: Maximum Product of Three Elements After One Replacement
class Solution {
public long maxProduct(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
long a = nums[0], b = nums[1];
long c = nums[n - 2], d = nums[n - 1];
final int x = 100000;
return Math.max(Math.max(a * b * x, c * d * x), -a * d * x);
}
}
// Accepted solution for LeetCode #3732: Maximum Product of Three Elements After One Replacement
func maxProduct(nums []int) int64 {
sort.Ints(nums)
n := len(nums)
a, b := int64(nums[0]), int64(nums[1])
c, d := int64(nums[n-2]), int64(nums[n-1])
const x int64 = 100000
return max(a*b*x, c*d*x, -a*d*x)
}
# Accepted solution for LeetCode #3732: Maximum Product of Three Elements After One Replacement
class Solution:
def maxProduct(self, nums: List[int]) -> int:
nums.sort()
a, b = nums[0], nums[1]
c, d = nums[-2], nums[-1]
x = 10**5
return max(a * b * x, c * d * x, a * d * -x)
// Accepted solution for LeetCode #3732: Maximum Product of Three Elements After One Replacement
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3732: Maximum Product of Three Elements After One Replacement
// class Solution {
// public long maxProduct(int[] nums) {
// Arrays.sort(nums);
// int n = nums.length;
// long a = nums[0], b = nums[1];
// long c = nums[n - 2], d = nums[n - 1];
// final int x = 100000;
// return Math.max(Math.max(a * b * x, c * d * x), -a * d * x);
// }
// }
// Accepted solution for LeetCode #3732: Maximum Product of Three Elements After One Replacement
function maxProduct(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
const [a, b] = [nums[0], nums[1]];
const [c, d] = [nums[n - 2], nums[n - 1]];
const x = 100000;
return Math.max(a * b * x, c * d * x, -a * d * x);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.