#K36502. Subarray Sum Equals K

    ID: 25768 Type: Default 1000ms 256MiB

Subarray Sum Equals K

Subarray Sum Equals K

Given an array of integers and a target integer \(k\), your task is to count the total number of continuous subarrays whose sum is exactly equal to \(k\).

A subarray is defined as a contiguous segment of the array. The sum of a subarray from index \(j\) to \(l\) is given by:

\(\displaystyle S = \sum_{i=j}^{l} a_i\)

Your solution should be efficient enough to handle large input sizes.

inputFormat

Input is read from standard input (stdin) and has the following format:

  • 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 elements.
  • The third line contains an integer \(k\), the target sum.

outputFormat

Output a single integer to standard output (stdout): the total number of continuous subarrays whose sum equals \(k\).

## sample
5
1 2 3 4 5
5
2