CJCoding With Joseph
15free / daySign inCreate account

Largest Subarray Sum

Write max_subarray_sum(values: list[int]) -> int that returns the largest sum of any run of one or more adjacent elements in a single pass (Kadane's method): keep current, the best sum of a run that ends at the element you are on, and best, the largest current seen so far. For each value after the first, current becomes the previous current plus the value when that is larger than the value alone, otherwise the run starts afresh at the value; then best is updated when current beats it. The list has at least one value and the answer may be negative. This is O(n), against O(n squared) for the brute-force version. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one line of space-separated integers; the tests print the value your function returns.

Expected Output:

6

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsBig-O
📥 Auto-Input:
-2 1 -3 4 -1 2 1 -5 4\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.