#K82322. Count of Subarrays with Target Sum

    ID: 35950 Type: Default 1000ms 256MiB

Count of Subarrays with Target Sum

Count of Subarrays with Target Sum

You are given an array of integers \(A = [A_1, A_2, \dots, A_N]\) and an integer target \(S\). Your task is to count the number of contiguous subarrays (i.e. subsegments) such that the sum of the elements in the subarray is exactly \(S\).

Formally, count the number of pairs \((i, j)\) satisfying \(1 \leq i \leq j \leq N\) for which:

[ \sum_{k=i}^{j} A_k = S ]

The solution should read from stdin and write the result to stdout.

inputFormat

The input is given via standard input as follows:

  • The first line contains an integer \(N\), the number of elements in the array.
  • The second line contains \(N\) space-separated integers representing the array \(A\).
  • The third line contains an integer \(S\), the target sum.

outputFormat

Output a single integer representing the number of contiguous subarrays whose sum equals \(S\). The output should be written to standard output.

## sample
5
1 2 3 4 5
5
2