Maximum Sum Path in Two Arrays | Practice | GeeksforGeeks

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. You can start from any array and switch from one array to another array only at common elements. 
Example 1:
Input:
M = 5, N = 4
Arr1[] = {2, 3, 7, 10, 12}
Arr2[] = {1, 5, 7, 8}
Output: 35
Explanation: 35 is sum of 1 + 5 + 7 + 10 +
12. We start from the first element of
Arr2 which is 1, then we move to 5, then 7
From 7, we switch to Arr1 (as 7 is common)
and traverse 10 and 12.
Example 2:
Input:
M = 2, N = 3
Arr1[] = {10, 12}
Arr2[] = {5, 7, 9}
Output: 22
Explanation: 22 is the sum of 10 and 12.
Since there is no common element, we need
to take all elements from the array with
more sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes two arrays of integers arr1, arr2, m and n as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(M+N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= M, N <= 105
0 <= Arr1[i], Arr2[i] <= 106
 
0
rohithchittibommala123
This comment was deleted.
0
gaurlakshya24263 months ago
C++ Optimal Solution →
Execution Time :0.01
 
int maxPathSum(int arr1[], int arr2[], int m, int n) {
	    // code here
	    int i=0,j=0,result=0,sum1=0,sum2=0;
	    
	    while(i<m && j<n){
	        if(arr1[i]<arr2[j]){
	            sum1+=arr1[i];
	            i++;
	        }
	        else if(arr1[i]>arr2[j]){
	            sum2+=arr2[j];
	            j++;
	        }
	        else{
	            result+=max(sum1,sum2)+arr1[i];
	            sum1=0;
	            sum2=0;
	            i++;
	            j++;
	        }
	        
	    } 
	    while(i<m) sum1+=arr1[i++];
	    while(j<n) sum2+=arr2[j++];
	    
	    result+=max(sum1,sum2);
	    return result;
	}
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.16
class Solution{    public:    int maxPathSum(int arr1[], int arr2[], int M, int N)    {        int res = 0;                int i = 0, j = 0;        int sum1 = 0, sum2 = 0;                while(i < M and j < N)        {            if(arr1[i] < arr2[j])            {                sum1 += arr1[i++];            }            else if(arr1[i] > arr2[j])            {                sum2 += arr2[j++];            }            else            {                res += max(sum1, sum2) + arr1[i];                                sum1 = sum2 = 0;                                i++;                j++;            }        }                while(i < M)        {            sum1 += arr1[i++];        }                while(j < N)        {            sum2 += arr2[j++];        }                res += max(sum1, sum2);                return res;    }};
0
abhikarsh gupta1 year ago
abhikarsh gupta
C++ O(M + N)
int maxPathSum(int arr1[], int arr2[], int m, int n) {    int sum1 = 0, sum2 = 0, maxsum = 0;    int i = 0 , j = 0;    while(i < m && j < n)    {        if(arr1[i] < arr2[j])            sum1 += arr1[i++];        else if(arr1[i] == arr2[j])        {            maxsum += max(sum1, sum2) + arr1[i];            i++; j++;            sum1 = 0;            sum2 = 0;        }        else            sum2 += arr2[j++];    }    while(i < m)        sum1 += arr1[i++];    while(j < n)        sum2 += arr2[j++];    maxsum += max(sum1, sum2);    return maxsum;}
0
uday chavan2 years ago
uday chavan
java soln 0.73
class Solution {    int maxPathSum(int[] arr1, int[] arr2, int m, int n) {        int sum1=0, sum2=0;        int i=0, j=0,result=0;        while(i<m &&="" j<n){="" if(arr1[i]<arr2[j])="" sum1="" +="arr1[i++];" else="" if(arr1[i]="">arr2[j])            sum2 += arr2[j++];            else{                result += Math.max(sum1,sum2);                sum1=0;                sum2=0;                while(i<m &&="" j<n="" &&="" arr1[i]="=arr2[j]){" result="" +="arr1[i++];" j++;="" }="" }="" }="" while(i<m)="" sum1="" +="arr1[i++];" while(j<n)="" sum2="" +="arr2[j++];" result="" +="Math.max(sum1,sum2);" return="" result;="" }="" }="">
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.