**Problem Description**

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 **common elements**.

**NOTE:**

- The common elements do not have to be at the same indexes.

1 <= |A|, |B| <= 10^{5}

1 <= A[i], B[i] <= 10^{3}

First argument is an integer array **A**.

Second argument is an integer array **B**.

Return a single integer denoting the maximum sum path.

Input 1:

A = [2, 3, 7, 10, 12] B = [1, 5, 7, 8]

Input 2:

A = [10, 12] B = [5, 7, 9]

Output 1:

35

Output 2:

22

Explanation 1:

35 is sum of 1 + 5 + 7 + 10 + 12. We start from the first element of B which is 1, then we move to 5, then 7. From 7, we switch to A (as 7 is common) and traverse 10 and 12.

Explanation 2:

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.

NOTE:You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.

Sign Up

to access hints and editorial solutions for**Maximum Path in Arrays**

to access hints and editorial solutions for

Loading...