P4 · 2 seconds · 256 MB

Maximum Subarray

Given an integer array `nums`, find the contiguous subarray with the largest sum and return that sum. The array contains at least one number (the answer can be negative if every element is negative).

function maxSubArray(nums) → number

CONSTRAINTS

  • 1 ≤ nums.length ≤ 10⁴
  • -10⁵ ≤ nums[i] ≤ 10⁵
Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: 6
[4, -1, 2, 1] sums to 6
Input: nums = [1]
Output: 1
Input: nums = [5, 4, -1, 7, 8]
Output: 23
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.