- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMaxSubarray.java
39 lines (30 loc) · 723 Bytes
/
MaxSubarray.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
importjava.util.*;
importjava.io.*;
classMain {
publicstaticintMaxSubarray(int[] arr) {
// code goes here
ArrayList<Integer> arlist = newArrayList<Integer>();
for(inti=0;i<arr.length;i++){
arlist.add(arr[i]);
}
intsum =0;
intmax=0;
for(inti=0;i<arlist.size();i++){
for(intj=i+1;j<=arlist.size();j++){
sum=0;
for(Integerk: arlist.subList(i,j)){
sum+=k;
if(sum>max){
max=sum;
}
}
}
}
returnmax;
}
publicstaticvoidmain (String[] args) {
// keep this function call here
Scanners = newScanner(System.in);
System.out.print(MaxSubarray(s.nextLine()));
}
}