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.

  1. 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.
  2. For each subarray, calculate their value by sum all its elements.
  3. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!