Problem
Ask
Submissions

Problem: Get the Maximum Score

Hard
40 min
Explore how to use the two pointers technique to traverse two sorted arrays, switching at common elements to maximize the unique sum score. Understand the problem constraints and implement a solution optimizing for large input sizes. This lesson helps you master array traversal and score calculation with efficient switching logic.

Statement

You are given two sorted arrays of distinct integers, nums1 and nums2.

A valid path is constructed according to the following rules:

  • You start at the index 00 of either nums1 or nums2.

  • From there, you must traverse the chosen array from left to right.

  • Suppose you encounter a number in both arrays (a common element, not necessarily at the same index). In that case, you may choose to switch to the other array at that element and continue the traversal from that point forward.

  • A common element can only be counted once in the total path score, regardless of the array it appears.

The score of a path is defined as the sum of all unique elements visited during traversal. Your task is to return the maximum possible score achievable by any valid path. As the answer may be too large, return the maximum score modulo 109+710^{9}+7.

Constraints:

  • 1 \leq nums1.length, nums2.length \leq 10510^{5}

  • 1 \leq nums1[i], nums2[i] \leq 10710^{7}

  • All elements in nums1 and nums2 are strictly increasing.

Problem
Ask
Submissions

Problem: Get the Maximum Score

Hard
40 min
Explore how to use the two pointers technique to traverse two sorted arrays, switching at common elements to maximize the unique sum score. Understand the problem constraints and implement a solution optimizing for large input sizes. This lesson helps you master array traversal and score calculation with efficient switching logic.

Statement

You are given two sorted arrays of distinct integers, nums1 and nums2.

A valid path is constructed according to the following rules:

  • You start at the index 00 of either nums1 or nums2.

  • From there, you must traverse the chosen array from left to right.

  • Suppose you encounter a number in both arrays (a common element, not necessarily at the same index). In that case, you may choose to switch to the other array at that element and continue the traversal from that point forward.

  • A common element can only be counted once in the total path score, regardless of the array it appears.

The score of a path is defined as the sum of all unique elements visited during traversal. Your task is to return the maximum possible score achievable by any valid path. As the answer may be too large, return the maximum score modulo 109+710^{9}+7.

Constraints:

  • 1 \leq nums1.length, nums2.length \leq 10510^{5}

  • 1 \leq nums1[i], nums2[i] \leq 10710^{7}

  • All elements in nums1 and nums2 are strictly increasing.