FastPrepMaximum Subarray Sum

Maximum Subarray Sum

Mygate logoMygate● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a nonempty integer array nums, return the largest sum of any nonempty contiguous subarray.

A subarray consists of consecutive elements. Values may be negative. Return the sum, rather than the subarray indices.

Function

maximumSubarraySum(nums: int[]) → long

Examples

Example 1

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

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

Example 2

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

The nonempty subarray [-3] is optimal; an empty subarray is not allowed.

Example 3

nums = [5]return = 5

The only nonempty subarray contains 5.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The answer fits a signed 64-bit integer.

More Mygate problems

See Mygate hiring insights
public long maximumSubarraySum(int[] nums) {
    // write your code here
}
nums[-2,1,-3,4,-1,2,1,-5,4]
expected6
Checking account…