Timer is Running

Max sum path in two arrays
Submissions: 15824   Accuracy:

41.41%

  Difficulty: Easy   Marks: 2

Given two sorted arrays A and B, such that the arrays may have some common elements. Find the sum of the maximum sum path to reach from the beginning of any array to end of any of the two arrays. We can switch from one array to another array only at the common elements.

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,4}
B[] = {1,2,7}
Output: 10
Explanation: The path will be 1+2+7=10.

Your Task:
Complete the function max_path_sum() that takes the two arrays A and B along with their sizes M and N as input parameters. It returns the sum of the maximum sum path.

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

Constraints:
1 <= M,N <= 105
1 <= A[i], B[i] <= 106

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal
Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.