c++ 100%
IntuitionThe problem requires us to find the maximum sum path between two sorted arrays, where we can switch paths at common elements. Since both arrays are sor
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to find the maximum sum path between two sorted arrays, where we can switch paths at common elements. Since both arrays are sorted, a two-pointer approach is an efficient way to traverse and accumulate sums while handling intersections.



# Approach
<!-- Describe your approach to solving the problem. -->
Initialize Pointers & Sums:

Use two pointers i and j to traverse nums1 and nums2 respectively.
Maintain two sum variables a and b to track sums along each path.
Traverse Both Arrays Simultaneously:

If nums1[i] < nums2[j], add nums1[i] to a and move i.
If nums1[i] > nums2[j], add nums2[j] to b and move j.
If nums1[i] == nums2[j] (common element), we must choose the maximum sum path so far and continue from that point.
Set both a and b to max(a, b) + nums1[i]
Move both pointers.
Compute Final Maximum:

Since the arrays might have different lengths, we add remaining elements in either list to their respective sums.
Return max(a, b) % mod to ensure the result does not exceed integer limits.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Since we traverse both arrays once, the time complexity is O(N + M), where N and M are the sizes of nums1 and nums2.


- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1), as we use only a few integer variables.


# Code
```cpp []
class Solution {
public:
          int maxSum(vector<int>& A, vector<int>& B) {
        int i = 0, j = 0, n = A.size(), m = B.size();
        long a = 0, b = 0, mod = 1e9 + 7;
        while (i < n || j < m) {
            if (i < n && (j == m || A[i] < B[j])) {
                a += A[i++];
            } else if (j < m && (i == n || A[i] > B[j])) {
                b += B[j++];
            } else {
                a = b = max(a, b) + A[i];
                i++, j++;
            }
        }
        return max(a, b) % mod;
    }
};
```
['C++']