#K72262. Count Subarrays with Target Sum
Count Subarrays with Target Sum
Count Subarrays with Target Sum
Given an array of integers arr and an integer k, your task is to compute the total number of continuous subarrays whose sum equals k. A subarray is defined as a contiguous portion of the array.
You can use the following mathematical formulation: if we define the prefix sum as \(S(i) = \sum_{j=0}^{i} arr[j]\), then the sum of a subarray from index \(i\) to \(j\) is \(S(j) - S(i-1)\) (with \(S(-1)=0\)). The problem asks you to count the number of pairs \((i, j)\) such that \(S(j) - S(i-1) = k\).
inputFormat
The first line of input contains two integers n and k, where n is the number of elements in the array and k is the target sum. The second line contains n space-separated integers representing the array.
outputFormat
Output a single integer that represents the total number of continuous subarrays whose sum equals k.## sample
5 5
1 2 3 2 5
3