-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-3.java
50 lines (36 loc) · 1.01 KB
/
Problem-3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> print all subarrays with 0 sum
Problem Link :- https://practice.geeksforgeeks.org/problems/stickler-theif-1587115621/1
Reference:- https://www.youtube.com/watch?v=C9-n_H7dsvU
*/
class Solution{
//Function to count subarrays with sum equal to 0.
public static long findSubarray(long[] arr ,int n)
{
//Your code here
HashMap<Long, Long> hmap=new HashMap<>();
long sum=0;
long total=0;
//to consider 0 sum as well
hmap.put((long)0,(long)1);
for(int i=0;i<n;i++)
{
sum+=arr[i];
if(!hmap.containsKey(sum))
{
hmap.put(sum,(long)1);
}
else
{
long count=hmap.get(sum);
total+=count;
count++;
hmap.put(sum,count);
}
}
return total;
}
}