Problems

Maximum Subarray Sum

MediumArrays

Given an array of integers, write a program to find the maximum sum of a subarray within the array.

Input:

  • The first line contains the number of elements in the array.
  • The second line contains the elements of the array separated by spaces.

Output:

  • The maximum sum of a subarray.

Example 1

Input: 5 -2 1 -3 4 -1

Output: 4

The maximum sum of a subarray is 4, which is the sum of the subarray [4].

Constraints

  • 1 <= n <= 10^5
  • -10^5 <= a[i] <= 10^5

Hints

arrayssubarraysum
Maximum Subarray Sum | CodeForge AI