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 size elements.
  • 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) = 9
Returns (int) 6

Example 2

Input
nums (int[]) = [5,4,-1,7,8]
size (int) = 5
Returns (int) 23

Example 3

Input
nums (int[]) = [-3,-2,-5]
size (int) = 3
Returns (int) -2

At 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

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.