628. Maximum Product of Three Numbers LeetCode Solution
In this guide, you will get 628. Maximum Product of Three Numbers LeetCode Solution with the best time and space complexity. The solution to Maximum Product of Three Numbers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Product of Three Numbers solution in C++
- Maximum Product of Three Numbers solution in Java
- Maximum Product of Three Numbers solution in Python
- Additional Resources

Problem Statement of Maximum Product of Three Numbers
Given an integer array nums, find three numbers whose product is maximum and return the maximum product.
Example 1:
Input: nums = [1,2,3]
Output: 6
Example 2:
Input: nums = [1,2,3,4]
Output: 24
Example 3:
Input: nums = [-1,-2,-3]
Output: -6
Constraints:
3 <= nums.length <= 104
-1000 <= nums[i] <= 1000
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
628. Maximum Product of Three Numbers LeetCode Solution in C++
class Solution {
public:
int maximumProduct(vector<int>& nums) {
const int n = nums.size();
ranges::sort(nums);
return max(nums[n - 1] * nums[0] * nums[1],
nums[n - 1] * nums[n - 2] * nums[n - 3]);
}
};
/* code provided by PROGIEZ */
628. Maximum Product of Three Numbers LeetCode Solution in Java
class Solution {
public int maximumProduct(int[] nums) {
final int n = nums.length;
Arrays.sort(nums);
return Math.max(nums[n - 1] * nums[0] * nums[1], nums[n - 1] * nums[n - 2] * nums[n - 3]);
}
}
// code provided by PROGIEZ
628. Maximum Product of Three Numbers LeetCode Solution in Python
class Solution:
def maximumProduct(self, nums: list[int]) -> int:
nums.sort()
return max(nums[-1] * nums[0] * nums[1],
nums[-1] * nums[-2] * nums[-3])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.