/325. Maximum Size Subarray Sum Equals k

325. Maximum Size Subarray Sum Equals k

Medium
Arrays50.8% acceptance

Given an integer array nums and an integer target_sum, return the length of the longest contiguous subarray whose sum equals target_sum. If there is no such subarray, return 0.

Example 1

Input: nums = [2, 4, -2, 1, 3, -1], target_sum = 5

Output: 3

Explanation: The subarray [4, -2, 3] sums to 5 and has length 3.

Example 2

Input: nums = [1, -1, 5, -2, 3], target_sum = 3

Output: 4

Explanation: The subarray [1, -1, 5, -2] sums to 3 and has length 4.

Example 3

Input: nums = [1, 2, 3], target_sum = 7

Output: 0

Explanation: No subarray sums to 7.

Constraints

  • 1 <= len(nums) <= 105
  • -104 <= nums[i] <= 104
  • -109 <= target_sum <= 109
Python (current runtime)

Case 1

Input: nums = [3, 1, -1, 2, 5], target_sum = 6

Expected: 4

Case 2

Input: nums = [-2, 1, 2, -1, 2], target_sum = 2

Expected: 5

Case 3

Input: nums = [0, 0, 0, 0], target_sum = 0

Expected: 4

Case 4

Input: nums = [7, -3, 2, 1, 4], target_sum = 5

Expected: 3

Case 5

Input: nums = [1, 2, 3, 4], target_sum = 10

Expected: 4