Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Sunday, April 30, 2023

[Solved] How to Find maximum Product of a sub-array in Java? Example

Hello guys, if you are asked to find maximum product of a sub-array in Java and you are wondering how to solve this difficult looking coding problem then you have come to the right place. Earlier, I have shared 30 array coding problems, 30 linked list problems, and 10 dynamic programming problems and in this article, we shall be finding the maximum product of a sub-array in java. This problem has a lot to do with arrays. But first, we'll solve the problem then we talk about Array and how it operates. The problem we are to solve here is to find the maximum product of a subarray in Java. We won't just jump to the writing of codes like code monkeys but, it is essential to understand the problem we are solving first, so we can give the best approach in solutions to it. when we say finding the maximum product of a sub-array it means getting the total of a product in an array!

Simple, right? but what if we have negative numbers in an array? how do we go about it? do we still find the product of all? we would definitely get a negative as a result and of course, a negative number can never be the maximum. so, this implementation we are about to carry out provides a good way to handle that.

Java Program to find the Maximum Product of a sub array

Here is the complete Java program to find the maximum product of a sub array in Java, you can copy paste this program in your favorite Java IDE like Eclipse, or IntelliJIDEA or may be even on NetBeans to play around.

public static int maxSubarrayProduct(int[] numbers) {
    int finalResult = numbers[0];
    for (int i = 0; i < numbers.length; i++) {
        int currentResult = numbers[i];
        for (int j = i + 1; j < numbers.length; j++) {
            finalResult = Math.max(finalResult, currentResult);
            currentResult *= numbers[j];
        // updating the result for (n-1)th index.
        finalResult = Math.max(finalResult, currentResult);
    return finalResult;


In line 1 there was a static method that returns an integer. it took in a parameter , an array of numbers. in line 2 we initialized a variable that was declared of type int with the first item in the array. the variable would be returned later. 

Followed by a for loop and inside the loop was another variable named current result which keeps track of the result in the loop. Note that this variable is different from the initial one and we are not returning this one.

There is another loop again, because we need to update current result every time to keep an eye over the maximum product which is the final result that we declared initially. so in line 6 the maximum between the current result and final result was decided by using Math.max() method and the result was saved in final variable too. the product was calculated and the first loop was updated by getting the maximum. Line 12 returned the final result.

Here is the driver class which will run the code and calculate the maximum product for a given sub-array:

public static void main(String[] args) {
 int[] arr = {6, -3, -10, 0, 2};
    System.out.println("Maximum Sub array product is " +

Line 1 here is the main method ,line 3 was a declared and initialized array with numbers. The method max sub array product was called with the right argument. it was passed into system. out for it to be printed.

The result of this gives us 180, 6 multiplied by -3 is -18 and -18 multiplied by -10 gives 180(Remember - * - = +). so 180 is the maximum product we have is 180

Having implemented the solution to the problem, Now we shall be discussing what an array is as a collection of elements of the same value. when I say the same value it means that you can not find different kinds of elements or data types there. 

[Solved] How to find the maximum Product of a sub-array in Java? Example

You can see above that we only have an array of integers, they are numbers. In Java, when you are creating an array newly you must do two important things:

Firstly, you must say the type of element coming in. whether it's an integer or a string or a Boolean etc. The good thing is that once you specified the data type that is coming you can not do otherwise. you cant declare an int array and now populate it with a string element, No!, it doesn't work that way. It leads to a compilation error.

Secondly, when declaring an array in java, you must say the length that's coming. And that too can never change. You cant say the length of items coming in at creation is 10 and eventually, you put 11 items, it won't work. 

This leads to an array index out of bounds exception. below is how to declare an array:

int [] numbers= new int [6];

The above "int []" tells us that it is an int array.
"numbers" is the name of the array and we are assigning 6 spaces i.e length to the numbers. it can only take 6 numbers.

That's all about how to find the maximum product sub array in Java. You learned how to find the maximum sub-array of a product in java. And array it self was briefly explained. Which helps you understand how it works, why and when to use it. You can always think of another approach get the same task done.

Other Array and Coding Problems you may like

  • How to sort an array in place in Java? (solution)
  • How to solve coin change problem in Java? (solution)
  • Top 5 Websites for Coding Interview Preparation (websites)
  • How to check if an array contains a particular value? (solution)
  • 7 Best Courses to learn Data Structure and Algorithms? (courses)
  • Prime Number Generation Algorithms - Sieve of Eratosthenes (algorithm)
  • How to find all pairs in an array whose sum is equal to k (solution)
  • Top 20 String Coding Problems from Interviews (questions)
  • How to remove an element from an array in Java? (solution)
  • How to find duplicates from an unsorted array in Java? (solution)
  • How to find the largest and smallest number in an array without sorting? (solution)
  • How to find one missing number in a sorted array? (solution)
  • How to remove duplicates from an array in Java? (solution)
  • 10 Courses to Learn Data Structure and Algorithms for Interviews (courses)
  • 75 Programming Questions to Crack Any Coding Interview (list)
  • Binary Search Algorithm without Recursion (algorithm)
  • 20+ Array-Based Coding Problems from interviews (questions)

Thanks for reading this article so far. If you like this array-based coding interview problem about finding maximum product of a subarray in Java and my solution and explanation then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

No comments :

Post a Comment