FastPrepMaximum Size Subarray Sum Equals K

Maximum Size Subarray Sum Equals K

eBay logoeBay● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an integer array nums and an integer k, return the maximum length of a contiguous nonempty subarray whose elements sum to k.

Return 0 if no such subarray exists.

Function

maxSubArrayLen(nums: int[], k: int) → int

Examples

Example 1

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

The subarray [1,-1,5,-2] sums to 3.

Example 2

nums = [-2,-1,2,1]k = 1return = 2

The subarray [-1,2] has length 2 and sum 1.

Constraints

  • 1 <= nums.length <= 2 * 10^5.
  • Values and prefix sums fit in a 64-bit signed integer.

More eBay problems

See eBay hiring insights
public int maxSubArrayLen(int[] nums, int k) {
  // write your code here
}
nums[1,-1,5,-2,3]
k3
expected4
Checking account…