-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximum_sub_array.java
More file actions
40 lines (34 loc) · 902 Bytes
/
Copy pathMaximum_sub_array.java
File metadata and controls
40 lines (34 loc) · 902 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
35
36
37
38
39
40
// User function Template for Java
class Solution {
ArrayList<Integer> findSubarray(int a[], int n) {
int max=0,start=0,end=-1,i=0,j=0,sum=0;
while(j<n){
if(a[j]>=0){
sum+=a[j];
j++;
}else{
if(sum>max){
max=sum;
start=i;
end=j-1;
}
sum=0;
j++;
i=j;
}
}
//extra
if(sum>max){
start=i;
end=j-1;
}
ArrayList<Integer> list=new ArrayList<>();
for(int k=start;k<=end;k++){
list.add(a[k]);
}
if(list.size()==0){
list.add(-1);
}
return list;
}
}