Skip to main content
Back to problems
#1464
Easy Algorithms

Maximum product of two elements in an array

Array Sorting Heap (Priority Queue)
83.5% acceptance
Feb 25, 2026
2612
239
Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn max_product(mut nums: Vec<i32>) -> i32 {
    nums.sort_unstable_by(|a, b| b.cmp(a));
    (nums[0] - 1) * (nums[1] - 1)
  }
}