[C++/Java] | Two Pointer(beats 100%) | O(M+N) time, O(1) space
C++ Solution:\n\nclass Solution {\npublic:\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        \n        int n = nums1.size();\n        int m = n
**C++ Solution:**\n```\nclass Solution {\npublic:\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        \n        int n = nums1.size();\n        int m = nums2.size();\n        int i=0, j=0;\n        long si=0, sj=0;\n        long res = 0;\n        \n        while(i<n && j<m)\n        {\n            if(nums1[i] < nums2[j])\n            {\n                si += nums1[i];\n                i++;\n            }\n            else if(nums1[i] > nums2[j])\n            {\n                sj += nums2[j];\n                j++;\n            }\n            else\n            {\n                res += max(si, sj) + nums1[i];\n                i++, j++;\n                si=0, sj=0;\n            }\n        }\n        \n        while(i<n)\n        {\n            si += nums1[i];\n            i++;\n        }\n        \n        while(j<m)\n        {\n            sj += nums2[j];\n            j++;\n        }\n        \n        return (max(si, sj) + res) % 1000000007;\n        \n    }\n};\n```\n\n**Java Solution:**\n```\nclass Solution {\n    public int maxSum(int[] nums1, int[] nums2) {\n        \n        int n = nums1.length;\n        int m = nums2.length;\n        int i=0, j=0;\n        long si=0, sj=0;\n        long res = 0;\n        \n        while(i<n && j<m)\n        {\n            if(nums1[i] < nums2[j])\n            {\n                si += nums1[i];\n                i++;\n            }\n            else if(nums1[i] > nums2[j])\n            {\n                sj += nums2[j];\n                j++;\n            }\n            else\n            {\n                res += Math.max(si, sj) + nums1[i];\n                i++; \n                j++;\n                si=0;\n                sj=0;\n            }\n        }\n        \n        while(i<n)\n        {\n            si += nums1[i];\n            i++;\n        }\n        \n        while(j<m)\n        {\n            sj += nums2[j];\n            j++;\n        }\n        \n        return (int)((Math.max(si, sj) + res) % 1000000007);\n        \n    }\n}\n```
['Two Pointers', 'C', 'C++', 'Java']