-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAx_subarray.java
More file actions
34 lines (28 loc) · 785 Bytes
/
MAx_subarray.java
File metadata and controls
34 lines (28 loc) · 785 Bytes
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
import java.util.HashMap;
public class MAx_subarray{
public static int count_the_len(int arr[] , int n){
HashMap<Integer,Integer> mpp = new HashMap<>();
int maxi = 0;
int sum_1 = 0;
for(int i=0; i<n;i++){
sum_1 += arr[i];
if(sum_1 == 0){
maxi = i+1;
}
else{
if(mpp.get(sum_1) != null){
maxi = Math.max(maxi,i - mpp.get(sum_1));
}else{
mpp.put(sum_1,i);
}
}
}
return maxi;
}
public static void main(String[] args){
int arr[] = {1,2,3,4};
int n = arr.length;
System.out.println(count_the_len(arr,n));
}
}
//