-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxsubarray.java
More file actions
33 lines (26 loc) · 857 Bytes
/
Copy pathMaxsubarray.java
File metadata and controls
33 lines (26 loc) · 857 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
import java.util.*;
public class Maxsubarray{
public static void maxsubarray(int numbers[]){
int currentSum = 0;
int MaxSum = Integer.MIN_VALUE;
for(int i = 0; i<numbers.length; i++){
int start = i;
for(int j=i; j<numbers.length; j++){
int end = j;
currentSum = 0;
for(int k = start; k<= end; k++){
currentSum += numbers[k];
}
System.out.println(currentSum);
if(MaxSum < currentSum) {
MaxSum = currentSum;
}
}
}
System.out.println("max sum = " + MaxSum);
}
public static void main(String [] args){
int numbers[] = {1, -2, 6, -1, 3};
maxsubarray(numbers);
}
}