Given an integer array nums, find a subarray that has the largest product, and return the product.
The test cases are generated so that the answer will fit in a 32-bit integer.
OR
Given an integer array with negative numbers and zero find the subarray with maximum product, i.e. find the contiguous array elements that produce maximum product.
Whenever a 0 appears, it splits the array into separate subarrays. We need to find the maximum product for each of these subarrays and return the largest product. Within each subarray, if there is an even number of negative elements, the maximum product is the product of the entire subarray. If there is an odd number of negative elements, form two subarrays—one excluding the leftmost negative element and the other excluding the rightmost negative element. The larger product of these two will be the result.
classSolution {
publicintmaxProductNaive(int[] nums) {
int ans = Integer.MIN_VALUE; // Start with smallest possible valueint n = nums.length;
for (int i = 0; i < n; i++) {
int prod = 1;
for (int j = i; j < n; j++) {
prod *= nums[j];
ans = Math.max(ans, prod);
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defmaxProductNaive(self, nums: List[int]) -> int:
ans: int = float('-inf') # Start with smallest possible value n: int = len(nums)
for i in range(n):
prod: int =1for j in range(i, n):
prod *= nums[j]
ans = max(ans, prod)
return ans
This is similar to maximum subarray sum. Instead of sum, the sign of number affect the product value.
We can use maxEndingHere and minEndingHere to keep track of positive and negative product so far. But, wherever there is 0, our product becomes 0. So, we reset these 2 variables. We need to find the maximum product of these subarrays individually and return the largest product.
Inside this subproblem if there are even number of negative elements then maximum product is the complete product of the array. If there are odd number of negative elements, then make two subarrays once leaving the leftmost negative element and once leaving the rightmost negative element. The maximum of these two products will be returned.
publicclassSolution {
publicintmaxProduct(int[] nums) {
int n = nums.length;
// Edge case: When the array is emptyif (n == 0) {
return 0;
}
// max positive product ending at the current positionint maxEndingHere = nums[0];
// min (negative) product ending at the current positionint minEndingHere = nums[0];
// Initialize overall max product to the first elementint maxSoFar = nums[0];
// Traverse through the array, starting from the second elementfor (int i = 1; i < n; i++) {
int current = nums[i];
// If the current element is negative, swap maxEndingHere and minEndingHereif (current < 0) {
int temp = maxEndingHere;
maxEndingHere = minEndingHere;
minEndingHere = temp;
}
// Update maxEndingHere and minEndingHere maxEndingHere = Math.max(current, maxEndingHere * current);
minEndingHere = Math.min(current, minEndingHere * current);
// Update the global max product maxSoFar = Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
}
At each index i, update the maximum and minimum values. Keeping track of the minimum is crucial because multiplying a negative number with the minimum value can turn it into the maximum, and vice versa. For each index i, calculate the maximum of the i-th element, prevMax multiplied by the i-th element, and prevMin multiplied by the i-th element.
classSolution {
publicintmaxProduct(int nums[]) {
int max = nums[0], min = nums[0], ans = nums[0];
for (int i = 1; i<n; i++) {
int temp = max; // store the max because before updating min your max will already be updatedint num = nums[i];
max = Math.max(Math.max(max * num, min * num), num);
min = Math.min(Math.min(temp * num, min * num), num);
ans = Math.max(ans, max);
}
return maxSoFar;
}
}
classSolution:
defmaxProduct(self, nums: List[int]) -> int:
# Initialising variables max_product: int = nums[0]
min_product: int = nums[0]
ans: int = nums[0]
# Iterating through the array starting from index 1for num in nums[1:]:
temp_max: int = max_product # Store the current max before updating# Update max_product and min_product max_product = max(
num, num * max_product, num * min_product
)
min_product = min(
num, num * temp_max, num * min_product
)
# Update the final answer ans = max(ans, max_product)
return ans