#628
Easy Algorithms Maximum product of three numbers
Array Math Sorting
45.7% acceptance
Feb 20, 2026
4525
715
Given an integer array nums, find three numbers whose product is maximum
and return the maximum product.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn maximum_product(mut nums: Vec<i32>) -> i32 {
nums.sort_unstable();
let n = nums.len();
// Either top 3, or two smallest negatives * largest
(nums[n-1] * nums[n-2] * nums[n-3])
.max(nums[0] * nums[1] * nums[n-1])
}
}