#C3988. Counting Subarrays with a Given Sum

    ID: 47475 Type: Default 1000ms 256MiB

Counting Subarrays with a Given Sum

Counting Subarrays with a Given Sum

You are given an array of integers and an integer ( k ). Your task is to count the number of contiguous subarrays whose elements sum exactly to ( k ). A subarray is defined as a contiguous segment of the array.

For example, if the array is [1, 2, 3, 4, 5] and ( k = 9 ), there are exactly 2 subarrays that sum to 9.

inputFormat

The input is taken from standard input (stdin) and consists of three parts:
1. An integer ( n ) representing the number of elements in the array.
2. A line with ( n ) space-separated integers representing the array elements.
3. An integer ( k ) representing the target sum.

outputFormat

Output a single integer on standard output (stdout), which is the number of contiguous subarrays that sum exactly to ( k ).## sample

5
1 2 3 4 5
9
2