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:
1
2
3
4
5
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.
To maximize (nums[i] - 1) * (nums[j] - 1), we want the two largest numbers in the array. Subtract 1 from each and multiply. Since all numbers are positive, we only need to track the two largest elements.
If negative numbers were allowed:
If the array could contain negative numbers, the maximum product could also come from the two smallest (most negative) numbers, since their product is positive. In that case, we would need to track both the two largest and two smallest numbers, and return the maximum of (max1-1)(max2-1) and (min1-1)(min2-1).