BackeasyArrays

Subarray Sum Matches Solution

Problem Statement

Given an array of integers and a target sum, count all subarrays where the sum of their elements equals the target sum.

Example 1
Input
[1, 2, 3, 4, 5] and target sum 5
Output
2

Explanation: Subarrays [2, 3] and [5] have sums equal to the target sum 5.

Example 2
Input
[10, 20, 30, 40] and target sum 60
Output
1

Explanation: Subarray [10, 20, 30] has a sum equal to the target sum 60.

Example 3
Input
[7, 7, 7] and target sum 14
Output
2

Explanation: Subarrays [7, 7] have sums equal to the target sum 14.

Constraints

  • 1 <= array length <= 1000
  • -10^5 <= array elements <= 10^5
  • -10^6 <= target sum <= 10^6
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