Program to find the maximum score from all possible valid paths in Python

Suppose we have two arrays nums1 and nums2. A valid path is defined as follows −

  • Select nums1 or nums2 to traverse (from index-0).

  • Traverse the array from left to right.

Now, if we are moving through any value that is present in nums1 and nums2 we can change the path to the other array. Here the score is the sum of unique values in a valid path. We have to find the maximum score we can get of all possible valid paths. If the answer is too large then return result modulo 10^9+7.

So, if the input is like nums1 = [3,5,6,9,11] nums2 = [5,7,9,10], then the output will be 35 because −

  • Valid paths starting from nums1 are: [3,5,6,9,11], [3,5,6,9,10], [3,5,7,9,10], [3,5,7,9,11]

  • Valid paths starting from nums2 are: [5,7,9,10], [5,6,9,11], [5,6,9,10], [5,7,9,11]

3 5 6 9 11 5 7 9 10 nums1: nums2: Optimal Path: [3,5,7,9,11] Sum: 3 + 5 + 7 + 9 + 11 = 35

So the maximum is [3,5,7,9,11] with sum 35.

Algorithm

To solve this, we will follow these steps −

  • M := size of nums1 , N := size of nums2

  • sum1 := 0, sum2 := 0 (track current path sums)

  • i := 0, j := 0 (pointers for both arrays)

  • res := 0 (accumulate maximum score)

  • while i < M and j < N, do

    • if nums1[i] < nums2[j], then add nums1[i] to sum1 and increment i

    • otherwise when nums1[i] > nums2[j], then add nums2[j] to sum2 and increment j

    • otherwise (equal values), choose the maximum path, reset sums, and continue

  • Add remaining elements from both arrays

  • return (res + maximum of remaining sums) mod 10^9+7

Example

Let us see the following implementation to get better understanding −

def solve(nums1, nums2):
    M, N = len(nums1), len(nums2)
    sum1, sum2 = 0, 0
    i, j = 0, 0
    res = 0
    
    while i < M and j < N:
        if nums1[i] < nums2[j]:
            sum1 += nums1[i]
            i += 1
        elif nums1[i] > nums2[j]:
            sum2 += nums2[j]
            j += 1
        else:
            # Equal values - choose maximum path and switch
            res += max(sum1, sum2) + nums1[i]
            i += 1
            j += 1
            sum1 = 0
            sum2 = 0
    
    # Add remaining elements
    while i < M:
        sum1 += nums1[i]
        i += 1
    while j < N:
        sum2 += nums2[j]
        j += 1
    
    return (res + max(sum1, sum2)) % 1000000007

# Test with given example
nums1 = [3, 5, 6, 9, 11]
nums2 = [5, 7, 9, 10]
print(solve(nums1, nums2))
35

How It Works

The algorithm uses a two-pointer approach to track both arrays simultaneously. At each common element (switching point), we choose the path with the higher sum accumulated so far. This greedy approach ensures we get the maximum possible score.

For the example [3,5,6,9,11] and [5,7,9,10]:

  • At element 5: sum1=3, sum2=0, choose sum1 and add 5 ? res=8

  • Continue: sum1=6 (from 6), sum2=7 (from 7)

  • At element 9: sum1=6, sum2=7, choose sum2 and add 9 ? res=8+7+9=24

  • Remaining: sum1=11, sum2=10, choose sum1 ? final=24+11=35

Conclusion

This problem demonstrates dynamic path selection using a greedy two-pointer approach. The key insight is choosing the maximum accumulated sum at each switching point to achieve the optimal result.

Updated on: 2026-03-26T14:09:01+05:30

663 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements