-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLargestSubarraySumZero.java
More file actions
87 lines (62 loc) · 2.09 KB
/
LargestSubarraySumZero.java
File metadata and controls
87 lines (62 loc) · 2.09 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*Problem Description
You are given an array arr of size N consisting of integer values.
You need to print the largest subarray which has its integer sum equal to zero i.e., with index i and j (i<=j), arr[i] + arr[i+1] + .. + arr[j] = 0 . If there is more than one subarray with the largest length then print the sub-array among it with the least starting index.
If there is no such sub-array print -1.
Input format
First line contains an integer N.
Next line consists of N integers, the values of the array.
Output format
Print the elements of the sub-array with spaces which have the largest length and sum of elements equal to zero.
Sample Input 1
6
2 3 1 -4 0 6
Sample Output 1
3 1 -4 0
Explanation
The largest subarray with sum zero is (3, 1, -4, 0)
Constraints
1 <= N <= 10^5
-10^4 <= arr[i] <= 10^4
*/
import java.util.*;
class LargestSubarraySumZero {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
ArrayList<Integer> res = largestSubarraySumZero(n, arr);
for (int j : res)
System.out.print(j + " ");
}
static ArrayList<Integer> largestSubarraySumZero(int n, int arr[]) {
HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
ArrayList<Integer> al = new ArrayList<Integer>();
if(arr.length == 1 ) al.add(-1);
int len = 0;
int sum= 0;
int start=1, end=-1;
hmap.put(0, -1);
for(int i=0; i< arr.length; i++){
sum += arr[i];
if(hmap.containsKey(sum)){
int l = hmap.get(sum) + 1;
if(i-l > end-start){
end = i;
start= l;
}
}else{
hmap.put(sum,i);
}
}
if(start == -1) return al;
for(int i =start; i<= end; i++){
al.add(arr[i]);
}
if(al.size() ==0){
al.add(-1);
}
return al;
}
}