Problem · Array

Maximum Subarray

Learn this problem
EasySquare Point logoSquare PointFULLTIMEONSITE INTERVIEW

Problem statement

You are given an integer array nums. Find the non-empty contiguous subarray with the largest sum and return that sum.

Function

maxSubArray(nums: int[]) → int

Examples

Example 1

nums = [-2,1,-3,4,-1,2,1,-5,4]return = 6

The best contiguous subarray is [4,-1,2,1], whose sum is 6.

Constraints

  • 1 <= nums.length <= 100000
  • -10000 <= nums[i] <= 10000
  • The answer fits in a signed 32-bit integer.

More Square Point problems

drafts saved locally
public int maxSubArray(int[] nums) {
  // write your code here
}
nums[-2,1,-3,4,-1,2,1,-5,4]
expected6
checking account