Max sum path in two arrays | Practice | GeeksforGeeks

Given two sorted arrays A and B of size M and N respectively. Each array may have some elements in common with the other array. Find the maximum sum of a path from the beginning of any array to the end of any of the two arrays. We can switch from one array to another array only at the common elements which are present at the same index.
Note: Only one repeated value is considered in the valid path sum.

Example 1:
Input:
M = 5, N = 4
A[] = {2,3,7,10,12}
B[] = {1,5,7,8}
Output: 35
Explanation: The path will be 1+5+7+10+12
= 35.


Example 2:
Input:
M = 3, N = 3
A[] = {1,2,3}
B[] = {3,4,5}
Output: 15
Explanation: The path will be 1+2+3+4+5=15.

Your Task:
You don't need to read input or print anything. Complete the function max_path_sum() which takes the two arrays A and B along with their sizes M and N as input parameters. It returns the maximum path sum.

Expected Time Complexity: O(M + N)
Expected Auxiliary Space: O(1)

Constraints:
1 <= M,N <= 104
1 <= A[i], B[i] <= 104
0
fls70015 days ago
This question is different and the test cases are not designed as per the question asked. Question says same index but according to test cases common elements can be at any index
0
bokonist1 week ago
Don't attempt this question before it's fixed and waste your time. Question is wrong. You should assume that common elements may be at any index.For example, this case:
4 46 7 8 91 2 3 6The answer would be 6+7+8+9 if the switch can happen only at same indices. But, the answer is 36 because the switch can happen at ‘6’ which is at A[0] and at B[3]
0
gautambaddy1 week ago
//please make it correct if possible
int sum1=0;  int sum2=0;  int sum11=0;  int sum22=0;  int i=0;  int k=0;  while((i<n)|(k<m)){      if(arr[i]>arr2[k]){          k++;          sum2=sum2+arr2[k];      }      else if(arr[i]<arr2[k]){          i++;          sum1=sum1+arr[i];      }      else if(arr[i]==arr2[k]){          break;      }      else{          return -1;      }        }  i=i+1;  k=k+1;  while(i<n){      sum11=sum11+arr[i];      i++;  }  while(k<m){      sum22=sum22+arr2[k];  }  if(sum1>sum2){      if(sum11>sum22){          return(sum1+sum11);      }      else{          return(sum1+sum22);      }        }  else{       if(sum11>sum22){          return(sum2+sum11);      }      else{          return(sum2+sum22);      }      }
0
cshubham4391 week ago
int n = ar1.length;
        int m = ar2.length;
        int i = 0;
        int j = 0;
        int sum1 = 0;
        int sum2 = 0;
        int result = 0;
        while(i<n && j<m){
            if(ar1[i]<ar2[j]){
                sum1+=ar1[i++];
            }
           else if(ar1[i]>ar2[j]){
                sum2+=ar2[j++];
            }
            else{
                result += Math.max(sum1,sum2)+ar1[i];
                i++;
                j++;
                sum1 = 0;
                sum2 = 0;
            }
        }
        while(i<n){
            sum1+=ar1[i++];
        }
        while(j<m){
            sum2+=ar2[j++];
        }
        result+=Math.max(sum1,sum2);
        return result;
+4
geminicode3 weeks ago
must be medium level.
+2
badgujarsachin833 weeks ago
 int max_path_sum(int A[], int B[], int l1, int l2)
    {
        int i=0,j=0,path1=0,path2=0;
        while(i<l1 && j<l2){
            if(A[i]<B[j]){
                path1+=A[i++];
            }else if(A[i]>B[j]){
                path2+=B[j++];
            }else{
                path1+=A[i++];
                path2+=B[j++];
                path1=max(path1,path2);
                path2=max(path1,path2);
            }
        }
        while(i<l1){
            path1+=A[i++];
        }
        while(j<l2){
            path2+=B[j++];
        }
        return max(path1,path2);
    }
-1
aniketsaraswat1124 weeks ago
int max_path_sum(int arr1[], int arr2[], int l1, int l2){
        int sum1 = 0, sum2 = 0, i = 0, j = 0, result = 0;
        while(i < l1 && j < l2){
            if(arr1[i] < arr2[j]) sum1 += arr1[i++];
            else if(arr2[j] < arr1[i]) sum2 += arr2[j++];
            else{
                result += max(sum1, sum2) + arr1[i];
                sum1 = sum2 = 0;
                i++,j++;
            }
        }
        while(i < l1) sum1 += arr1[i++];
        while(j < l2) sum2 += arr2[j++];
        return result + max(sum1,sum2);
    }
+4
siricoolent274 weeks ago
This questions test cases aren't appropriate
+1
rishabhjain169
This comment was deleted.
0
r__________h2 months ago
 int max_path_sum(int A[], int B[], int l1, int l2)   {       int res=0;       int i=0,j=0;       int asum=0,bsum=0;       while(i<l1 && j<l2){           if(A[i]==B[j]){                                          asum+=A[i++];               bsum+=B[j++];               res+=max(asum,bsum);                            asum=0;               bsum=0;           }           if(A[i]<B[j])               asum+=A[i++];           if(A[i]>B[j])               bsum+=B[j++];       }       while(i<l1){           asum+=A[i++];       }       while(j<l2){           bsum+=B[j++];       }       res+=max(asum,bsum);       return res;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.