BackeasyArrays

Sequential Sum Frequency Solution

Problem Statement

Given an array of integers and a target sum, calculate the number of contiguous subarrays where the sum of elements equals the target sum.

Example 1
Input
{"arr":[1,2,3,4,5],"targetSum":5}
Output
2

Explanation: Subarrays [2, 3] and [5] have a sum of 5.

Example 2
Input
{"arr":[10,20,30,40,50],"targetSum":60}
Output
1

Explanation: Subarray [10, 20, 30] has a sum of 60.

Example 3
Input
{"arr":[5,5,5,5,5],"targetSum":10}
Output
4

Explanation: Subarrays [5, 5], [5, 5], [5, 5], and [5, 5] have a sum of 10.

Constraints

  • 1 <= arr.length <= 10^5
  • -10^4 <= arr[i] <= 10^4
  • -10^5 <= targetSum <= 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