Question: SOLVE IN JAVA In this application, you will write an application to find the max subarray. Well say each subarray has a value, the value
SOLVE IN JAVA
In this application, you will write an application to find the max subarray.
Well say each subarray has a value, the value is the sum of all its elements. You need to find the max subarray value in the given array. The challenging part of this question is, you may have negative numbers in your array.
For example, the largest subarray section in {1, 2, -5, 3, 8, 9, -20, 3, 2, 1}. Obviously the max subarray are {3,8,9} with a value = 20
Hint: Divide and conquer.
- Find the all possible of subarrays. You can find all subarrays by using a nested for-loop to iterate all possible beginning positions and all possible ending positions.
- For each subarray, calculate their value by sum all its elements.
- Use a maxValue to store the max value you found.
maxSubarray([1, 2, 3]) 6
maxSubarray([1, 2, -5, 3, 8, 9, -20, 3, 2, 1]) 20
maxSubarray([1, 2, -5, 3, 8, 9, -5, 3, 2, 1]) 21
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
