Intermediate Level
Implement the method maxSubarraySum that returns the largest sum of any contiguous subarray.
A subarray is a continuous block of the original array. The answer must use at least one element.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizeelements. - Do not reorder elements.
- All-negative arrays should return the largest negative value.
Example 1
Input
nums (int[]) =
[-2,1,-3,4,-1,2,1,-5,4]size (int) =
9Returns (int)
6Example 2
Input
nums (int[]) =
[5,4,-1,7,8]size (int) =
5Returns (int)
23Example 3
Input
nums (int[]) =
[-3,-2,-5]size (int) =
3Returns (int)
-2At each position, decide whether to extend the previous subarray or start a new one. Track the best sum seen.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run