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
Maximum Product of Two ElementsInput: [3, 4, 5, 2]3452Pick two largest: 5 and 445Calculate: (5-1) × (4-1) = 4 × 3Output: 12
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
Asked in
Amazon 15 Microsoft 8
25.0K Views
Medium Frequency
~10 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen