Maximum Product of Two Elements in an Array

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).

Example 1
Inputnums = [3,4,5,2]
Output12
Choosing indices i = 1 and j = 2 gives (4 - 1) * (5 - 1) = 3 * 4 = 12, which is the maximum value.
Example 2
Inputnums = [1,5,4,5]
Output16
Choosing indices i = 1 and j = 3 gives (5 - 1) * (5 - 1) = 16, which is the maximum value.

Constraints

  • 2 <= nums.length <= 500
  • 1 <= nums[i] <= 10^3

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate