DIY: Subarray Sum Equals K
Solve the interview question "Subarray Sum Equals K" in this lesson.
We'll cover the following
Problem statement
In this scenario, you will be provided with an array of integers and a single integer k
. Your task is to find the total number of contiguous subarrays whose sum is equal to the integer value of k
.
Input
The input is an array of integers and k
. The following is an example input:
arr = {1,2,3,4,5,6,7,1,23,21,3,1,2,1,1,1,1,1,12,2,3,2,3,2,2}
k = 2
Output
The output is the total number of subarrays whose sum is equal to k
. The following is an example output:
10
Coding exercise
You need to implement the function subarraySum(arr, k)
, where arr
is an array of integers and k
is the value you need to check for. The function returns a single integer value representing the number of contiguous subarrays whose sum is equal to k
.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.