← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Given an integer array nums and an integer k, return the number of contiguous, non-empty subarrays whose elements sum to k. The array may contain negative numbers, zeros, and positive numbers, so any sliding-window technique that relies on monotonicity does not apply. Instead, use prefix sums and a hash map that records how many times each prefix-sum value has been seen so far. As you scan the array from left to right, the number of new valid subarrays that end at the current index is exactly the number of previous prefix sums equal to (current_prefix_sum − k). Accumulate this count on every step and update the hash map with the current prefix sum before moving on.