#C10057. Subarray Sum Equals K

    ID: 39220 Type: Default 1000ms 256MiB

Subarray Sum Equals K

Subarray Sum Equals K

You are given an array of integers and a target integer ( k ). Your task is to determine the number of contiguous subarrays whose sum equals ( k ). This problem requires efficient handling of cumulative sums and hash mapping to achieve an optimal solution.

For example, given the array [1, 1, 1] and ( k = 2 ), there are 2 subarrays whose sum equals 2: [1, 1] from the first two elements and [1, 1] from the last two elements.

inputFormat

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

  • The first line contains an integer \( n \) representing 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 \) representing the target sum.

outputFormat

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

## sample
3
1 1 1
2
2