I am having trouble implementing max sub array problem using divide and conquer.
Lets say I have an array [3,6,-1,2] and I want to find the max sum of this array in contiguous order. We can look at this and see that the sum is 10 from [0,3].
I tried implementing the pseudo code from my book but the answer is not correct.
// return (max-left, max-right, max sum left + right)
public static int[] maxcross(int[] array, int low, int mid, int high) {
int leftSum = -10000000;
int rightSum = -10000000;
int sum = 0;
int maxLeft=0;
int maxRight=0;
for(int i=mid;i<mid-low;i--){
sum = sum + array[i];
if(leftSum < sum){
leftSum = sum;
maxLeft = i;
}
}
sum=0;
for(int i=mid+1;i<high;i++){
sum = sum + array[i];
if(rightSum < sum){
rightSum = sum;
maxRight = i;
}
}
int cross[] = {maxLeft,maxRight,leftSum+rightSum};
return cross;
}
public static int[] maxsub(int array[], int low, int high){
int[] maxSubLeft = new int[3];
int[] maxSubRight = new int[3];
int[] maxSub = new int[3];
int[] maxSubCross = new int[3];
int mid;
if (high==low){
maxSub[0] = low;
maxSub[1] = high;
maxSub[2] = array[low];
return maxSub;
}
else{
mid = (int) Math.floor((low+high)/2);
maxSubLeft = maxsub(array,low,mid);
maxSubRight = maxsub(array,mid+1,high);
maxSubCross = maxcross(array,low,mid,high);
if(maxSubLeft[2] >= maxSubRight[2] && maxSubLeft[2] >= maxSubCross[2])
return maxSubLeft;
else if(maxSubRight[2] >= maxSubLeft[2] && maxSubRight[2] >= maxSubCross[2])
return maxSubRight;
else
return maxSubCross;
}
}
I am getting this as the output
1
1
6
Can someone help me?