JavaScript two pointer traversal 100% speed 100% memory
Idea is to concurrently traverse both arrays from front to back, stepping forward at each iteration in the array with the lower value number. Since the arrays a
Idea is to concurrently traverse both arrays from front to back, stepping forward at each iteration in the array with the lower value number. Since the arrays are sorted, this ensures that values shared by both arrays will be found.\n\nAt each step, add the value of the number to a running sum for that array. When the shared value is encountered, replace both running sums with the maximum between them since this is the point on a path where we can move to the other array. Continue traversal after adding the shared value to both path sums.\n\n```\nconst maxSum = (nums1, nums2) => {\n    const modulo = 1000000007;\n    let i = 0, j = 0, l1 = nums1.length, l2 = nums2.length, sum1 = 0, sum2 = 0;\n    while (i < l1 || j < l2){\n        if (i < l1 && (j === l2 || nums1[i] < nums2[j])){     //if value 2 is greater than value 1\n            sum1 += nums1[i];                                 //traverse path1 and read the next node in 1\n            i++;\n        } else if (j < l2 && (i === l1 || nums1[i] > nums2[j])) {   //if value 1 is greater than value 2\n            sum2 += nums2[j];                                       //traverse path2 and read the next node in 2\n            j++;\n        } else {                  //If the two values are equal\n            const newval = Math.max(sum1, sum2) + nums1[i];\n            sum1 = newval;        //set the sum at each node to the maximum observed so far\n            sum2 = newval;\n            i++;                  //read the next nodes\n            j++;\n        }\n    }\n    return Math.max(sum1,sum2) % modulo;   //return the maximum running path sum\n};\n```
[]