325. Maximum Size Subarray Sum Equals k
Practice Maximum Size Subarray Sum Equals k for free on NudgeCode: Medium, Array & Hash Table pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Given an integer array nums and an integer k, return the length of the longest contiguous subarray whose sum is exactly k. Return 0 if no subarray sums to k.
nums can contain negative numbers and zeros, so window sums don't grow steadily as the window gets longer.
Example
nums = [1,-1,5,-2,3], k = 3 -> 4 ([1,-1,5,-2])
nums = [-2,-1,2,1], k = 1 -> 2 ([-1,2])
Constraints
- 1 <= nums.length <= 2 * 10^5
- -10^4 <= nums[i] <= 10^4
- -10^9 <= k <= 10^9