Skip to main content
Back to problems
Leetcode
Easy
Arrays
Sorting
Greedy
Maximum Product Of Two Elements In An Array

Find the maximum value of (x1)(y1)(x-1)(y-1) over all pairs of distinct elements in an array.

Acceptance 100%
Problem Statement

Problem

Given an array of positive integers, choose two different elements xx and yy. Your task is to maximize the value of

(x1)×(y1)(x - 1) \times (y - 1)

Return the largest possible product.

Intuition

Since subtracting 1 from each number preserves ordering, the best pair will come from the two largest values in the array.

Input Format

  • An integer array nums of length at least 2.
  • Each element is a positive integer.

Output Format

  • Return a single integer: the maximum value of (x1)(y1)(x - 1)(y - 1) over all pairs of distinct elements.

Constraints

  • 2nums.length2 \le nums.length
  • 1nums[i]1 \le nums[i]
  • The answer fits in a 32-bit signed integer.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [3,4,5,2]

Output

12

Explanation

The two largest numbers are 5 and 4. The product is (5 - 1) * (4 - 1) = 12.

Example 2

Input

nums = [1,5,4,5]

Output

16

Explanation

The two largest numbers are 5 and 5. The product is (5 - 1) * (5 - 1) = 16.

Show 1 more example

Example 3

Input

nums = [3,7]

Output

12

Explanation

Only one pair exists: (3 - 1) * (7 - 1) = 12.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.