-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKadanes.java
30 lines (30 loc) · 1008 Bytes
/
Kadanes.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
import java.util.*;
public class Kadanes {
public static void main(String[] args) {
int ar[]={12,32,43,534,534,6546,456,-2323,123,-65,-45,53,54,4553,54,-54,-2343,32432,43242,-1232142};
int l=ar.length;
int sum=0;
int start=0,ansStart=-1,ansEnd=-1,max=Integer.MIN_VALUE;
for(int i=0;i<l;i++){
if(sum==0){
start=i;
}
sum+=ar[i];
// Check if the current sum is greater than the maximum sum found so far
if(sum > max){
// Update the maximum sum and the corresponding subarray indices
max = sum;
ansStart = start;
ansEnd = i;
}
if(sum<0){
sum=0;
}
}
System.out.println();
System.out.println("THE MAXIMUM SUBARRAY IS : ");
for(int i=ansStart;i<ansEnd;i++){
System.out.print(ar[i]+" ");
}
}
}