⌘K
Flycodx

Maximum Subarray Sum

Maximum Subarray Sum

EasyArrays64% accepted

Given an integer array of n elements, find the contiguous subarray (containing at least one number) that has the largest sum and return its sum. A contiguous subarray is a sequence of consecutive elements from the original array. You cannot skip elements. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer.

Input format — only if your code reads from stdin

Input format (stdin): - First line: integer n (the size of the array) - Second line: n space-separated integers Output: a single integer — the maximum subarray sum.

Approach

Apply Kadane's Algorithm: track a running sum and reset it when it drops below zero. Keep a global maximum updated at every step.

Input

9 -2 1 -3 4 -1 2 1 -5 4

Output

6
  • •1 ≤ n ≤ 10^5, -10^4 ≤ nums[i] ≤ 10^4
solution.py
Loading editor...
Test 1
Test 2
Test 3

Press Run to test your custom input, or Submit to judge against all test cases.

Sign in to continue

Create a free account to run code, submit solutions and track progress.

  • Free forever — no credit card
  • 10+ languages with live execution
  • Points, streaks & leaderboards
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3