FastPrepMaximum Subarray
Problem · Array

Maximum Subarray

Learn this problem
MediumGoogle logoGoogleINTERNPHONE SCREEN
See Google hiring insights

Problem statement

Given a non-empty integer array nums, return the maximum possible sum of a non-empty contiguous subarray.

The result is a signed 64-bit integer.

Function

maxSubarraySum(nums: int[]) → long

Examples

Example 1

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

The contiguous subarray [4, -1, 2, 1] has the maximum sum, 6.

Example 2

nums = [-8,-3,-6]return = -3

A valid subarray must be non-empty, so the best choice is the single value -3.

Constraints

  • 1 <= nums.length <= 200000
  • -10^9 <= nums[i] <= 10^9

More Google problems

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