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