Back to Practice Dashboard
Competitive ProgrammingEasy

Largest Sum Contiguous Subarray

Learn how to solve the 'Largest Sum Contiguous Subarray' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function max_subarray_sum(arr) that finds and returns the maximum sum of a contiguous subarray in an array of integers arr.

Constraints
  • 1 <= len(arr) <= 10^5
  • -10^4 <= arr[i] <= 10^4

Examples

Example 1
Input
max_subarray_sum([-2, -3, 4, -1, -2, 1, 5, -3])
Output
7
Explanation

The contiguous subarray with the maximum sum is [4, -1, -2, 1, 5], summing to 7.

Example 2
Input
max_subarray_sum([-1])
Output
-1
Explanation

The maximum subarray contains only the element -1.

Need a Hint?
Define subproblem states, establish the recurrence relation, and use memoization (top-down) or tabulation (bottom-up).
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor