Javatpoint Logo
Javatpoint Logo

Java Program for Maximum Product Subarray

In the field of computer programming, the maximum product subarray problem is a popular challenge that requires finding the contiguous subarray within an array of integers that has the largest product. This problem can be efficiently solved using dynamic programming techniques. In this article, we will explore a Java program that solves the maximum product subarray problem and discuss its implementation.

Problem Statement:

The maximum product subarray problem can be stated as follows: given an array of integers, we need to find the subarray with the maximum product of its elements. For example, given the array [2, 3, -2, 4], the maximum product subarray is [2, 3], which has a product of 6.

Approach:

To solve this problem, we can use dynamic programming to keep track of both the maximum and minimum product of subarrays ending at each position in the array. This approach is based on the fact that a negative number can turn into a positive number if multiplied by another negative number, so we need to keep track of both the maximum and minimum products.

Algorithm:

1. We initialize maxProduct, maxEndingHere, and minEndingHere variables to the first element of the array, as the maximum product subarray can at least be the first element itself.

2. We iterate through the array starting from the second element. For each element, we calculate the maximum and minimum product ending at the current position using the formulas:

The maxEndingHere variable is updated by considering the current element itself, multiplying it by the previous maximum product, and multiplying it by the previous minimum product. This ensures that we cover both positive and negative scenarios.

The minEndingHere variable is updated similarly, but it takes into account the possibility of a negative number resulting in a larger positive product.

3. We update maxProduct with the maximum product found so far, ensuring that it captures the overall maximum product subarray.

4. Finally, we return maxProduct, which represents the maximum product subarray within the given array.

The dynamic programming approach used in this program guarantees an efficient solution to the maximum product subarray problem, with a time complexity of O(n), where n is the size of the input array. This efficiency is achieved by iteratively updating the maximum and minimum product subarrays at each position, avoiding redundant calculations.

Java program for solving the maximum product subarray problem:

MaximumProductSubarray.java

Output:

Maximum product subarray: 6

Explanation:

  • We initialize maxProduct, maxEndingHere, and minEndingHere variables to the first element of the array since the maximum product subarray can at least be the first element itself.
  • We iterate through the array starting from the second element. For each element, we calculate the maximum and minimum product ending at the current position using the formulas:

We update maxProduct with the maximum product found so far.

  • Finally, we return maxProduct, which represents the maximum product subarray.

In the above example, the program will output Maximum product subarray: 6, correctly identifying the subarray [2, 3] as the maximum product subarray.

The dynamic programming approach used in this program ensures an efficient solution to the maximum product subarray problem, with a time complexity of O(n), where n is the size of the input array. The Java program presented in this article provides a robust solution to the maximum product subarray problem using dynamic programming techniques. By employing this program, programmers can efficiently find the subarray within an array of integers that yields the largest product.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA