Subarray Sum Equals K

Problem Given an integer array nums (values may be negative) and an integer k, count the contiguous subarrays whose sum equals exactly k.

Input / Output

  • Input: int array nums, int k. Output: count of subarrays.

Constraints

  • n up to 2*10^4 with negatives — sliding window is invalid (no monotonicity); O(n) hashmap expected.

Example

  • nums = [1,1,1], k = 2 → 2; nums = [1,-1,0], k = 0 → 3.
asked …
LeaderboardSalaryAccount