Maximum Product of Two Elements

Easy

Choose two different indices i and j that maximise (nums[i] - 1) * (nums[j] - 1) and return that maximum.

Example 1

maxProduct()
Input
nums = [3,4,5,2]
Output
12

Why

(5 - 1) * (4 - 1) = 12

Example 2

maxProduct()
Input
nums = [1,5,4,5]
Output
16
Constraints
2 <= nums.length <= 500
1 <= nums[i] <= 10^3

Topics

ArraysSorting

Implement maxProduct in the editor and return the answer — printing it is never graded.