Given two sorted arrays such the arrays may have some common elements. Find the sum of the maximum sum path to reach from beginning of any array to end of any of the two arrays. We can switch from one array to another array only at common elements.
Note : Imp to execute and trace to understand and remember
package testPackage;
public class maxPathSum{
public static void main(String args[]){
int a[]={2,3,7,8,10,11};
int b[] = {1,5,6,7,10,12};
int n = a.length;
int m = b.length;
int i=0,j=0,sum1=0,sum2=0,sum=0,max=0,startpoint1=0,startpoint2=0;
while(ib[j])
{
sum2=sum2+b[j];
j++;
}
else if(a[i]<b[j]) { sum1 = sum1 + a[i]; i++; } else if(a[i]==b[j]) { if(sum1>sum2)
{
max=sum1;
for(int p=startpoint1 ; p <=i ; p++)
System.out.print(" " + a[p]);
}
else
{
max = sum2;
for(int p=startpoint2 ; p <=j ; p++)
System.out.print(" " + b[p]);
}
startpoint2=j+1;
startpoint1=i+1;
sum = sum + max + a[i];
i++;
j++;
sum1=0;
sum2=0;
}
else
{
System.out.println("Error!");
}
}
while(i<n)
{
sum1 = sum1 + a[i];
i++;
}
while(j<m) { sum2 = sum2 + b[j]; j++; } if(sum1>sum2)
{
max=sum1;
for(int p=startpoint1 ; p

