public class MaxSub {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a={13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7};
int[] r=new int[3];
r=Find_Max_Sub(a, 0, a.length-1);
System.out.println(r[0]+" "+r[1]+" "+r[2]);
}
public static int[] Find_Max_Sub(int[] a,int low,int high){
int[][] result=new int[4][3];
if(low==high){
result[0][0]=low;
result[0][1]=high;
result[0][2]=a[low];
return result[0];
}
else{
int mid=(low+high)/2;
result[1]=Find_Max_Sub(a, low, mid);
result[2]=Find_Max_Sub(a, mid+1, high);
result[3]=Find_Cross_Sub(a, low, mid, high);
if(result[1][2]>=result[2][2]&&result[1][2]>=result[3][2])
return result[1];
else if(result[2][2]>=result[1][2]&&result[2][2]>=result[3][2])
return result[2];
else
return result[3];
}
}
public static int[] Find_Cross_Sub(int[] a,int low,int mid,int high){
int[] result=new int[3];
final int MAX=99999;
int sum=0;
int leftsum=-MAX;
int rightsum=-MAX;
int maxleft=0;
int maxright=0;
for(int i=mid;i>=low;i--){
sum+=a[i];
if(sum>leftsum){
leftsum=sum;
maxleft=i;
}
}
sum=0;
for(int i=mid+1;i<=high;i++){
sum+=a[i];
if(sum>rightsum){
rightsum=sum;
maxright=i;
}
}
result[0]=maxleft;
result[1]=maxright;
result[2]=leftsum+rightsum;
return result;
}
}