-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefixSumstoZero.java
53 lines (50 loc) · 1.46 KB
/
prefixSumstoZero.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
51
52
53
// Given an array of integers, both -ve and +ve, find a contiguous subarray that sums to 0.
// For example: [2,4,-2,1,-3,5,-3] --> [4,-2,1,-3]
// time - O(n)
// space - O(1)
import java.util.HashMap;
public class prefixSumstoZero {
public static int[] findSumZero(int[] a){
if(a == null || a.length == 0){
return null;
}
int[] ans = new int[2];
ans[0] = -1;
ans[1] = -1;
HashMap<Integer, Integer> map = new HashMap<>();
int sum = a[0];
map.put(sum, 0);
int ind = 1;
while (ind < a.length) {
sum = sum + a[ind];
if(sum == 0){
ans[0] = 0;
ans[1] = ind;
break;
}
if(map.containsKey(sum)){
ans[0] = map.get(sum) + 1;
ans[1] = ind;
break;
}
ind++;
}
return ans;
}
public static void main(String[] args) {
int[] inputArray = {1, 0, 0, 0, 0};
int[] resultArray = findSumZero(inputArray);
if(resultArray == null){
System.out.print("Empty input array");
}
else{
for(int i = 0 ; i < resultArray.length ; i++){
if(resultArray[i] == -1){
System.out.print("Subarray not available");
break;
}
System.out.print(resultArray[i] + " ");
}
}
}
}