Skip to contentKEEP THE CRAFTTHE SANCTUARY

Maximum Contiguous Subarray Sum

Find the largest sum of any contiguous, nonempty part of an array.

The problem

Implement Solution.maxSubArray(nums). nums holds between 1 and 100,000 integers, and each one may be negative. A subarray is a contiguous, nonempty slice of nums. Return the largest sum any subarray can have. For example, in [-2, 1, -3, 4, -1, 2, 1, -5, 4] the best subarray is [4, -1, 2, 1] with sum 6.

Starter code

class Solution:
    def maxSubArray(self, nums):
        pass

Public examples

  1. nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]gives6
  2. nums = [0, 0]gives0
  3. nums = [-10, 2, 3]gives5

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.