Java|| python||c++ || Two Pointer || EASY TO UNDERSTAND||
Intuition\n\nWe will use two pointers to traverse each array and two variables (sum1 and sum2) to keep track of the cumulative sums for each array. Whenever we 
# Intuition\n\nWe will use two pointers to traverse each array and two variables (sum1 and sum2) to keep track of the cumulative sums for each array. Whenever we encounter the same element in both arrays, we will assign sum1 = sum2 = Math.max(sum1, sum2) + nums1[i] to ensure we take the maximum possible sum path.\n\n# Approach\n1. Initialize two pointers, one for each array.\n2. Traverse the arrays with the following logic:\n- If nums1[i] < nums2[j], add nums1[i] to sum1 and increment i.\n- If nums1[i] > nums2[j], add nums2[j] to sum2 and increment j.\n- If the elements are equal, update both sums to the maximum of sum1 and sum2, add the current element, and increment both pointers.\n\n3. After the loop, add any remaining elements in either array to their respective sums.\n4. Return the maximum of the two sums modulo 10^9+7\n\n# Complexity\n\n**Time complexity:\n** \nO(m+n) since we traverse both arrays simultaneously.\n\n**Space complexity:\n**\nO(1) because no extra space is used.\n\n```java []\nclass Solution {\n    public int maxSum(int[] nums1, int[] nums2) {\n        int mod = (int)1e9+7;\n\n        int i=0, j=0;\n        long sum1=0, sum2=0;\n        int n = nums1.length, m = nums2.length;\n        \n        \n        while(i<n && j<m){\n            if(nums1[i] < nums2[j]){\n                sum1 += nums1[i++];\n            }    \n            else if(nums1[i] > nums2[j]){\n                sum2 += nums2[j++];\n            }\n            else{\n                sum1 = sum2 = Math.max(sum1, sum2) + nums1[i];\n                i++;\n                j++;\n            }\n\n        }\n\n        while(i<n){\n            sum1 += nums1[i++];\n        }\n\n        while(j<m){\n            sum2 += nums2[j++];\n        }\n\n        return (int)(Math.max(sum1, sum2) % mod);\n    }\n}\n```\n```python []\nclass Solution:\n    def maxSum(self, nums1, nums2):\n        mod = int(1e9+7)\n        \n        i, j = 0, 0\n        sum1, sum2 = 0, 0\n        n, m = len(nums1), len(nums2)\n        \n        while i < n and j < m:\n            if nums1[i] < nums2[j]:\n                sum1 += nums1[i]\n                i += 1\n            elif nums1[i] > nums2[j]:\n                sum2 += nums2[j]\n                j += 1\n            else:\n                sum1 = sum2 = max(sum1, sum2) + nums1[i]\n                i += 1\n                j += 1\n        \n        while i < n:\n            sum1 += nums1[i]\n            i += 1\n        \n        while j < m:\n            sum2 += nums2[j]\n            j += 1\n        \n        return int(max(sum1, sum2) % mod)\n\n```\n```c++ []\nclass Solution {\npublic:\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        int mod = 1e9 + 7;\n        \n        int i = 0, j = 0;\n        long long sum1 = 0, sum2 = 0;\n        int n = nums1.size(), m = nums2.size();\n        \n        while (i < n && j < m) {\n            if (nums1[i] < nums2[j]) {\n                sum1 += nums1[i++];\n            } else if (nums1[i] > nums2[j]) {\n                sum2 += nums2[j++];\n            } else {\n                sum1 = sum2 = max(sum1, sum2) + nums1[i];\n                i++;\n                j++;\n            }\n        }\n        \n        while (i < n) {\n            sum1 += nums1[i++];\n        }\n        \n        while (j < m) {\n            sum2 += nums2[j++];\n        }\n        \n        return static_cast<int>(max(sum1, sum2) % mod);\n    }\n};\n\n```\n\n
['Two Pointers', 'Python', 'C++', 'Java']