DSA SectionEasy

Largest Sum Contiguous SubArray

Detailed guide and Python implementation for the 'Largest Sum Contiguous SubArray' problem.

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?
Consider using Arrays-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.