AfterAcademy Tech
•
01 May 2020

Difficulty: Medium
Asked in: Amazon
Given an integer array nums[], write a program to find three numbers from the array whose product is maximum and output the result.
Problem Note
Example 1
Input: nums[] = [-5, -7, 4, 2, 1, 9]
Output: 315
Explanation: Max Product of 3 numbers = -5 * -7 * 9 = 315
Example 2
Input: nums[]= [4, 5, -19, 3]
Output: 60
Explanation: Max Product of 3 numbers = 4 * 5 * 3 = 60
We will be discussing three different solutions for this problem
You may try to solve this problem here Maximum Product of Three Numbers. Learning via problem-solving is the best way to crack any coding interview. So, just visit the link and try to solve using the above methods. If stuck, then you can find the solution below.
To find the maximum product of three numbers, we can find all the triplets of the array and multiply them to get the maximum result.
Solution steps
maxProduct and initialize it with INT_MINmaxProduct with max(maxProduct, product(triplet))Pseudo Code
int maximumProduct(int[] nums) {
n = nums.size
maxProduct = INT_MIN
for(int i = 0 to i < n-2) {
for(int j = i + 1 to j < n-1) {
for(int k = j + 1 to k < n) {
tripletProduct = nums[i] * nums[j] * nums[k]
maxProduct = max(maxProduct, tripletProduct)
}
}
}
return maxProduct
}
Complexity Analysis
Time Complexity: O(n³)
Space Complexity: O(1)
Critical Ideas to Think
maxProduct with INT_MIN?If you will think for a second, you may realize that the maximum product of any three numbers would be the product of the greatest three numbers or the smallest two numbers with the greatest number from the nums array.
The reason why the maximum product of any triplet could have two smallest numbers because there could be negative numbers in the nums array and the product of the two smallest numbers could be largely positive if the numbers are greater than the 2nd and 3rd largest number of the array.
So if we will sort the array in ascending order, then the largest three numbers will be available at the end of the array. Also, the smallest two numbers will be available at the beginning of the array.
Solution Step
nums array in ascending ordermaxProduct variable and initialize it with INT_MIN.maxProduct withmax(maxProduct, nums[0]*nums[1]*nums[n-1])max(maxProduct, nums[n-1]*nums[n-2]*nums[n-3])Pseudo Code
int maximumProduct(int[] nums) {
n = nums.size
maxProduct = INT_MIN
sort(nums)
maxProduct = max(maxProduct, nums[0]*nums[1]*nums[n-1))
maxProduct = max(maxProduct, nums[n-3]*nums[n-2]*nums[n-1))
return maxProduct
}
Complexity Analysis
Time Complexity: O(n log n), due to sorting.
Space Complexity: O(log n), Sorting stack space using merge sort or quicksort.
Critical Ideas To Think
maxProduct?maxProduct?(comment down below)If you have understood the second approach, then by this time you would have realized that only the three maximum values and two smallest values participate in finding the maximum product of three numbers in the nums array.
So, we can find the largest three values and the smallest two values of the array in a single pass of the nums array by using five variables.
Solution Step
INT_MIN that be used to store the maximum three values of the array.INT_MAX, that can be used to store the minimum two values of the array.Pseudo Code
int maximumProduct(int[] nums) {
min1 = INT_MAX
min2 = INT_MAX
max1 = INT_MIN
max2 = INT_MIN
max3 = INT_MIN
for (int i = 0 to i < nums.size) {
if (nums[i] <= min1) {
min2 = min1
min1 = nums[i]
} else if (nums[i] <= min2) { // n lies between min1 and min2
min2 = nums[i]
}
if (nums[i] >= max1) { // n is greater than max1, max2 and max3
max3 = max2
max2 = max1
max1 = nums[i]
} else if (nums[i] >= max2) { // n lies betweeen max1 and max2
max3 = max2
max2 = nums[i]
} else if (nums[i] >= max3) { // n lies betwen max2 and max3
max3 = nums[i]
}
}
return max(min1 * min2 * max1, max1 * max2 * max3)
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Critical Ideas to Think

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given an array arr[] consists of 0, 1, 2, 3, 4,.....n. But one of the numbers is missing in it. Write a program to find the number that is missing from the array.

AfterAcademy Tech
You are given an integer array arr of size n. Assume a sliding window of size k starting from index 0. In each iteration, the sliding window moves to the right by one position till n-k. Write a program to return an array representing the maximum number in all sliding windows.

AfterAcademy Tech
Given an array A[] of size n, you need to find the maximum and minimum element present in the array.Your algorithm should make minimum number of comparisons.

AfterAcademy Tech
In this blog, we will learn about data abstraction and how it achieved using its three levels. We will learn the working of all the three levels of data abstraction.
