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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program max_product
    implicit none

    integer, parameter :: n = 3
    integer, dimension(n) :: nums
    integer :: i, j, k
    integer :: max_prod

    ! Example 1
    nums = (/ 1, 2, 3 /)
    write (*, '(A, I0)') 'Example 1: ', max_product_sub(nums)

    ! Example 2
    nums = (/ 1, 2, 3, 4 /)
    write (*, '(A, I0)') 'Example 2: ', max_product_sub(nums)

    ! Example 3
    nums = (/ -1, -2, -3 /)
    write (*, '(A, I0)') 'Example 3: ', max_product_sub(nums)

contains

    function max_product_sub(nums) result(max_prod)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: i, j, k
        integer :: max_prod

        max_prod = 0
        do i = 1, size(nums) - 2
            do j = i + 1, size(nums) - 1
                do k = j + 1, size(nums)
                    if (nums(i) * nums(j) * nums(k) > max_prod) then
                        max_prod = nums(i) * nums(j) * nums(k)
                    end if
                end do
            end do
        end do
    end function max_product_sub

end program max_product
Compiled
Executed
Correct
```
program max_product
    implicit none
    integer, parameter :: int64 = selected_int_kind(13)
    integer, dimension(:), allocatable :: nums
    integer :: i, j, k, n, max_product

    ! Read the input
    read (*, *) n
    allocate (nums(n))
    do i = 1, n
        read (*, *) nums(i)
    end do

    ! Find the maximum product of three numbers
    max_product = 0
    do i = 1, n-2
        do j = i+1, n-1
            do k = j+1, n
                if (nums(i) * nums(j) * nums(k) > max_product) then
                    max_product = nums(i) * nums(j) * nums(k)
                end if
            end do
        end do
    end do

    ! Output the result
    write (*, *) max_product

end program max_product
```
🌐 Data from online sources
def maximumProduct(nums):
    nums.sort()
    n = len(nums)
    return max(nums[0] * nums[1] * nums[-1], nums[-1] * nums[-2] * nums[-3])

First, we sort the input array in non-decreasing order. After sorting, we can determine the maximum product using two possible scenarios.

  1. The maximum product is formed by the product of the three highest numbers in the array. We can get this by multiplying the last three elements of the sorted array.

  2. The maximum product is formed by the product of the two lowest (most negative) numbers and the highest number in the array. We can get this by multiplying the first two elements and the last element of the sorted array.

We take the maximum of these two results to get the final answer.

🌐 Data from online sources
int maximumProduct(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    int n = nums.size();
    return max(nums[0] * nums[1] * nums[n-1], nums[n-1] * nums[n-2] * nums[n-3]);
}

First, we sort the input array in non-decreasing order. After sorting, we can determine the maximum product using two possible scenarios.

  1. The maximum product is formed by the product of the three highest numbers in the array. We can get this by multiplying the last three elements of the sorted array.

  2. The maximum product is formed by the product of the two lowest (most negative) numbers and the highest number in the array. We can get this by multiplying the first two elements and the last element of the sorted array.

We take the maximum of these two results to get the final answer.