Skip to content

Instantly share code, notes, and snippets.

@gauravkr0071
Last active August 10, 2021 07:50
Show Gist options
  • Save gauravkr0071/3cb4bd97862408296a74f47b938a22c1 to your computer and use it in GitHub Desktop.
Save gauravkr0071/3cb4bd97862408296a74f47b938a22c1 to your computer and use it in GitHub Desktop.
Maximum Product of Three Numbers
/*
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
*/
// concept- Simply find out the three largest numbers and the two smallest numbers using one pass.
public int maximumProduct(int[] nums) {
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE, min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
for (int n : nums) {
if (n > max1) {
max3 = max2;
max2 = max1;
max1 = n;
} else if (n > max2) {
max3 = max2;
max2 = n;
} else if (n > max3) {
max3 = n;
}
if (n < min1) {
min2 = min1;
min1 = n;
} else if (n < min2) {
min2 = n;
}
}
return Math.max(max1*max2*max3, max1*min1*min2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment