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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).
Example 1:
Input: nums = [3,4,5,2] Output: 12 Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12.
Example 2:
Input: nums = [1,5,4,5] Output: 16 Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
Example 3:
Input: nums = [3,7] Output: 12
Constraints:
2 <= nums.length <= 5001 <= nums[i] <= 10^3Problem summary: Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[3,4,5,2]
[1,5,4,5]
[3,7]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1464: Maximum Product of Two Elements in an Array
class Solution {
public int maxProduct(int[] nums) {
int ans = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
ans = Math.max(ans, (nums[i] - 1) * (nums[j] - 1));
}
}
return ans;
}
}
// Accepted solution for LeetCode #1464: Maximum Product of Two Elements in an Array
func maxProduct(nums []int) int {
ans := 0
for i, a := range nums {
for _, b := range nums[i+1:] {
t := (a - 1) * (b - 1)
if ans < t {
ans = t
}
}
}
return ans
}
# Accepted solution for LeetCode #1464: Maximum Product of Two Elements in an Array
class Solution:
def maxProduct(self, nums: List[int]) -> int:
ans = 0
for i, a in enumerate(nums):
for b in nums[i + 1 :]:
ans = max(ans, (a - 1) * (b - 1))
return ans
// Accepted solution for LeetCode #1464: Maximum Product of Two Elements in an Array
impl Solution {
pub fn max_product(nums: Vec<i32>) -> i32 {
let mut max = 0;
let mut submax = 0;
for &num in nums.iter() {
if num > max {
submax = max;
max = num;
} else if num > submax {
submax = num;
}
}
(max - 1) * (submax - 1)
}
}
// Accepted solution for LeetCode #1464: Maximum Product of Two Elements in an Array
function maxProduct(nums: number[]): number {
const n = nums.length;
for (let i = 0; i < 2; i++) {
let maxIdx = i;
for (let j = i + 1; j < n; j++) {
if (nums[j] > nums[maxIdx]) {
maxIdx = j;
}
}
[nums[i], nums[maxIdx]] = [nums[maxIdx], nums[i]];
}
return (nums[0] - 1) * (nums[1] - 1);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.