#K49092. Subarray Sum Equals k

    ID: 28565 Type: Default 1000ms 256MiB

Subarray Sum Equals k

Subarray Sum Equals k

You are given an array of integers and an integer \( k \). Your task is to determine the total number of continuous subarrays within the given array that sum exactly to \( k \).

A continuous subarray is a sequence of elements from the original array, in order. Formally, given an array \( A[0\ldots n-1] \), you need to count the number of pairs \( (i, j) \) with \( 0 \leq i \leq j < n \) such that \[ \sum_{l=i}^{j} A[l] = k \]

This is a classic problem that requires an efficient solution, as the size of the input array can be large.

inputFormat

The input is given via standard input and consists of two lines:

  • The first line contains two space-separated 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 elements of the array.

outputFormat

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

## sample
3 2
1 1 1
2