## Algorithm

Problem Name: 628. 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`

## Code Examples

### #1 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int lastThree = nums[n - 1] * nums[n - 2] * nums[n - 3];
int firstTwoAndLast = nums[0] * nums[1] * nums[n - 1];
return Math.max(lastThree, firstTwoAndLast);
}
}
``````
Copy The Code &

Input

cmd
nums = [1,2,3]

Output

cmd
6

### #2 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const maximumProduct = function (nums) {
nums.sort((a, b) => a - b)
return Math.max(
nums[0] * nums[1] * nums[nums.length - 1],
nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3]
)
}
``````
Copy The Code &

Input

cmd
nums = [1,2,3]

Output

cmd
6

### #3 Code Example with Python Programming

```Code - Python Programming```

``````start codin
class Solution:
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
return max(nums[-1]*nums[-2]*nums[-3], nums[-1]*nums[0]*nums[1])
``````
Copy The Code &

Input

cmd
nums = [1,2,3,4]

Output

cmd
24

### #4 Code Example with C# Programming

```Code - C# Programming```

``````
using System;

namespace LeetCode
{
public class _0628_MaximumProductOfThreeNumbers
{
public int MaximumProduct(int[] nums)
{
int max1, max2, max3, min1, min2;
max1 = max2 = max3 = int.MinValue;
min1 = min2 = int.MaxValue;
foreach (var num in nums)
{
if (num >= max1)
{
max3 = max2;
max2 = max1;
max1 = num;
}
else if (num >= max2)
{
max3 = max2;
max2 = num;
}
else if (num > max3)
max3 = num;

if (num <= min1)
{
min2 = min1;
min1 = num;
}
else if (num <= min2)
min2 = num;
}

return Math.Max(min1 * min2 * max1, max1 * max2 * max3);
}
}
}
``````
Copy The Code &

Input

cmd
nums = [1,2,3,4]

Output

cmd
24