Maximum Product of Two Elements in an Array

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

30:00

Maximum Product of Two Elements in an Array
easy
Free written solution
Topics
Companies

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

Example 1:
Input: {"nums":[3,4,5,2]}
Output: 12
Constraints:
  • 2≤nums.length≤5002 \leq \text{nums.length} \leq 500

  • 1≤nums[i]≤1031 \leq \text{nums}[i] \leq 10^3

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,4,5,2]}

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

first

0

second

0

product as an area

(0 - 1) x (0 - 1)

0

Variables
VariableValue
n4
DepthFunction Call
Stack empty
0/6