Subarrays with given sum
Easy Accuracy: 34.7% Submissions: 5183 Points: 2

Given an unsorted array arr[] of N integers and a sum. The task is to count the number of subarrays which add to a given number.

Example 1:

Input:
N=5
sum=-10
arr[] = { 10, 2, -2, -20, 10 }
Output:  3
Explanation:
Subarrays with sum -10 are:
[10, 2, -2, -20], [2, -2, -20, 10]
and [-20, 10].


Example 2:

Input:
N=6
sum= 33
arr[] = { 1, 4, 20, 3, 10, 5 }
Output:  1


Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function subArraySum() that takes array arr , integer N and integer sum as parameters and returns the count of the subarray which adds to the given sum.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

Constraints:
1 ≤ N ≤ 106

We are replacing the old Disqus forum with the new Discussions section given below.

to report an issue on this page.

### Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?