PIXELBANKv9.1.0
Menu

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 locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Maximum Subarray - Medium | PixelBank