30:00
Given the array of integers nums, choose two different indices i and j and return the maximum value of (nums[i] - 1) * (nums[j] - 1).
nums
i
j
(nums[i] - 1) * (nums[j] - 1)
2≤nums.length≤5002 \leq \text{nums.length} \leq 5002≤nums.length≤500
1≤nums[i]≤1031 \leq \text{nums}[i] \leq 10^31≤nums[i]≤103
Run code to see results.
Every value is at least 1, so (a - 1) * (b - 1) only grows with a and b. The answer uses the two largest numbers.
nums, scanned once
first
second
3
4
5
2
leader slots
0
product as an area
(0 - 1) x (0 - 1)
Choose two different indices and maximise (nums[i] - 1) * (nums[j] - 1). Because every value is at least 1, the answer always comes from the two largest values.