#K93792. Subarray Sum Equals k
Subarray Sum Equals k
Subarray Sum Equals k
Given an array of integers nums
and an integer k
, your task is to find the total number of continuous subarrays whose sum equals to k. A subarray is a contiguous part of the array.
The problem can be formulated using prefix sums. Let \( prefix[i] \) denote the sum of the first \( i \) elements. A subarray from index \( j \) to \( i-1 \) has sum equal to \( k \) if:
\[ prefix[i] - prefix[j] = k \]Your solution should efficiently count all such subarrays.
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains two space-separated integers:
n
(the number of elements in the array) andk
(the target sum). - The second line contains
n
space-separated integers representing the elements of the array.
outputFormat
Output a single integer to standard output (stdout) which is the number of continuous subarrays whose sum equals to k
.
8 7
4 3 2 1 1 1 1 1
3