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
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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; } |
Explanation
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.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:
1 2 3 4 5 | public static void main(String[] args) { int[] arr = {6, -3, -10, 0, 2}; System.out.println("Maximum Sub array product is " + maxSubarrayProduct(arr)); } |
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.
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.
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