BackhardArrays

Subarray Sum Frequency Solution

Problem Statement

Given an integer array and an integer k, return the number of continuous subarrays where the sum of the elements equals k.

Example 1
Input
nums = [1, 4, 7, 2, 5], k = 8
Output
0

Explanation: Subarrays with sum 8 are [4, 4] (does not exist in this array), [1, 7], [7, 1], and [2, 5, 1] (does not exist in this array) does not meet the continuous subarray requirement but [1, 7] and [2, 5, 1] meet the requirement, [7, 1] meet the requirement

Example 2
Input
nums = [3, 1, 2, 6, 5, 4], k = 7
Output
0

Explanation: Subarrays with sum 7 are [3, 1, 2, 1] does not meet the requirement (it has extra 1 that does not exist) but [6, 1], [2, 5] meet the requirement

Example 3
Input
nums = [9, 7, 5, 3, 1], k = 15
Output
1

Explanation: Subarray with sum 15 is [9, 6] does not meet the requirement (it has 6 that does not exist) but [7, 5, 3] meet the requirement

Constraints

  • 1 <= nums.length <= 20000
  • -10^4 <= nums[i] <= 10^4
  • -10^5 <= k <= 10^5
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free