feat-Add MaximumProductSubarray (#6711)

* feat-Add MaximumProductSubarray

* feat-changes

* fix: correct MaximumProductSubarray compilation errors

* fix-lints

* fix: apply clang-format to MaximumProductSubarrayTest.java

* fix: prevent integer overflow in MaximumProductSubarray using long type
This commit is contained in:
Sachin Pangal
2025-10-09 12:42:51 +05:30
committed by GitHub
parent 4fe37c3a10
commit e22ce0005e
2 changed files with 210 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
package com.thealgorithms.dynamicprogramming;
/**
* The MaximumProductSubarray class implements the algorithm to find the
* maximum product of a contiguous subarray within a given array of integers.
*
* <p>Given an array of integers (which may contain positive numbers, negative
* numbers, and zeros), this algorithm finds the contiguous subarray that has
* the largest product. The algorithm handles negative numbers efficiently by
* tracking both maximum and minimum products, since a negative number can turn
* a minimum product into a maximum product.</p>
*
* <p>This implementation uses a dynamic programming approach that runs in O(n)
* time complexity and O(1) space complexity, making it highly efficient for
* large arrays.</p>
*/
public final class MaximumProductSubarray {
private MaximumProductSubarray() {
// Prevent instantiation
}
/**
* Finds the maximum product of any contiguous subarray in the given array.
*
* @param nums an array of integers which may contain positive, negative,
* and zero values.
* @return the maximum product of a contiguous subarray. Returns 0 if the
* array is null or empty.
*/
public static int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
long maxProduct = nums[0];
long currentMax = nums[0];
long currentMin = nums[0];
for (int i = 1; i < nums.length; i++) {
// Swap currentMax and currentMin if current number is negative
if (nums[i] < 0) {
long temp = currentMax;
currentMax = currentMin;
currentMin = temp;
}
// Update currentMax and currentMin
currentMax = Math.max(nums[i], currentMax * nums[i]);
currentMin = Math.min(nums[i], currentMin * nums[i]);
// Update global max product
maxProduct = Math.max(maxProduct, currentMax);
}
return (int) maxProduct;
}
}