Maximum Product of Two Elements in an Array - Problem
Given an 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).
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,4,5,2]
›
Output:
12
💡 Note:
Choose indices 1 and 2: (4-1) * (5-1) = 3 * 4 = 12
Example 2 — Minimum Size
$
Input:
nums = [1,5]
›
Output:
0
💡 Note:
Only two elements: (1-1) * (5-1) = 0 * 4 = 0
Example 3 — All Same Values
$
Input:
nums = [3,3,3,3]
›
Output:
4
💡 Note:
All elements equal: (3-1) * (3-1) = 2 * 2 = 4
Constraints
- 2 ≤ nums.length ≤ 500
- 1 ≤ nums[i] ≤ 103
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array of integers
2
Find Largest Two
Identify the two largest values
3
Calculate Product
Return (max1-1) * (max2-1)
Key Takeaway
🎯 Key Insight: Maximum product always comes from the two largest elements in the array
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code