PIXELBANKv8.2.1
Menu

Maximum Subarray

Given an integer array, find the subarray with the largest sum and return its sum.

Example:

Input:
-2,1,-3,4,-1,2,1,-5,4
Output:
6
Reasoning:
  • We start by considering all possible subarrays of the given input: -2, 1, -3, 4, -1, 2, 1, -5, 4
  • We calculate the sum of each subarray, looking for the maximum sum: the subarray 4, -1, 2, 1 has a sum of 4+(1)+2+1=64 + (-1) + 2 + 1 = 6
  • The sum of this subarray, 6, is greater than the sum of any other subarray, such as the subarray 4, -1, 2, 1, -5 which has a sum of 4+(1)+2+1+(5)=14 + (-1) + 2 + 1 + (-5) = 1
  • The final output is the maximum sum found, which is 6

Constraints:

  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4
Editor

Test Results

0/0
Run code to see test results.