
Subarray Sum Equals K (medium)
Problem Statement
Given an array nums containing n integers and integer k, return the total number of subarrays having sum equal to k.
A subarray is defined as a contiguous non-empty sequence of the array elements.
Examples
Example 1:
- Input:
nums = [1, 2, 3], k = 3 - Expected Output:
2 - Justification: There are two subarrays that sum to 3:
[1, 2]and[3].
Example 2:
- Input:
nums = [10, 2, -2, -20, 10], k = -10 - Expected Output:
3 - Justification: Three subarrays sum up to -10:
[10, 2, -2, -20],[2, -2, -20, 10], and[-20, 10].
Example 3:
- Input:
nums = [5, 1, 2, -3, 4, -2], k = 3 - Expected Output:
2 - Justification: There are two subarrays that sum to 3:
[2, -3, 4], and[1, 2].
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory