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
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
temp.f95:14:4: 14 | nums = (/ 1, 2, 3, 4 /) | 1 Error: Different shape for array assignment at (1) on dimension 1 (3 and 4)
```
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
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:6:38: 6 | integer :: i, j, k, n, max_product | 1 Error: Symbol ‘max_product’ at (1) cannot have a type temp.f95:16:16: 16 | max_product = 0 | 1 Error: ‘max_product’ at (1) is not a variable temp.f95:20:62: 20 | if (nums(i) * nums(j) * nums(k) > max_product) then | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:21:32: 21 | max_product = nums(i) * nums(j) * nums(k) | 1 Error: ‘max_product’ at (1) is not a variable temp.f95:22:19: 22 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:28:28: 28 | write (*, *) max_product | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:31:2: 31 | ``` | 1 Error: Invalid character in name at (1) temp.f95:11:8: 11 | do i = 1, n | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:18:12: 18 | do j = i+1, n-1 | 1 Error: Symbol ‘j’ at (1) has no IMPLICIT type temp.f95:19:16: 19 | do k = j+1, n | 1 Error: Symbol ‘k’ at (1) has no IMPLICIT type temp.f95:9:17: 9 | read (*, *) n | 1 Error: Symbol ‘n’ at (1) has no IMPLICIT type
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.
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.
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.
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.
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.
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.