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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find three numbers in a list that, when multiplied together, give the biggest possible result. The brute force method involves simply checking every single combination of three numbers from the list.
Here's how the algorithm would work step-by-step:
def maximum_product_of_three_numbers_brute_force(numbers):
list_length = len(numbers)
if list_length < 3:
return None
maximum_product = float('-inf')
for first_number_index in range(list_length):
# Iterate through all possible first numbers
for second_number_index in range(first_number_index + 1, list_length):
for third_number_index in range(second_number_index + 1, list_length):
# Ensure unique triplets
product = numbers[first_number_index] * numbers[second_number_index] * numbers[third_number_index]
# Keep track of the largest product seen so far
if product > maximum_product:
maximum_product = product
return maximum_product
To find the largest product of three numbers, we don't need to check every combination. The key is to realize that the largest product will either come from the three largest numbers or from the two smallest (most negative) numbers multiplied by the largest number. We just need to keep track of these potential candidates.
Here's how the algorithm would work step-by-step:
def maximum_product_of_three_numbers(numbers):
numbers.sort()
# Get the three largest numbers.
largest_number_1 = numbers[-1]
largest_number_2 = numbers[-2]
largest_number_3 = numbers[-3]
# Get the two smallest numbers.
smallest_number_1 = numbers[0]
smallest_number_2 = numbers[1]
# Calculate product of three largest numbers.
product_of_largest_numbers = largest_number_1 * largest_number_2 * largest_number_3
# Calculate product of two smallest and one largest.
product_of_smallest_with_largest = smallest_number_1 * smallest_number_2 * largest_number_1
# Return the larger of the two products
return max(product_of_largest_numbers, product_of_smallest_with_largest)
Case | How to Handle |
---|---|
Empty or null input array | Return null or throw an IllegalArgumentException, as no product can be computed. |
Array with fewer than 3 elements | Return null or throw an IllegalArgumentException, since three numbers are required for the product. |
Array contains only positive numbers | The largest three numbers should be multiplied to get the maximum product. |
Array contains only negative numbers | The largest three (least negative) numbers should be multiplied to get the maximum product. |
Array contains a mix of positive, negative, and zero numbers | Consider both the product of the three largest numbers and the product of the two smallest (most negative) numbers and the largest number, returning the greater of the two. |
Array contains duplicate numbers | The algorithm should handle duplicate numbers correctly without any special adjustments. |
Integer overflow during product calculation | Use long data type to store the product to prevent potential overflow issues. |
Array contains MAX_INT/MIN_INT | Carefully choose the correct three elements to multiply such that the result is maximized without overflowing. |