Subarrays having a sum of p

Question

Suppose you have an unsorted array of integers. Can you find the number of subarrays that have a sum exactly to the given number p?

Input: 
array = [10, 2, -2, -20, 10]
k = -10
Output : 3
#The following subarrays [10, 2, -2, -20], [2, -2, -20, 10], [-20, 10] have a sum exactly equal to -10
                
Input: 
array = [9, 4, 20, 3, 10, 5]
k = 33
Output : 2
#The following subarrays [9, 4, 20], [20, 3, 10] have a sum exactly equal to 33

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now