Largest Forward Path Sum Across Two Increasing Arrays

Quick Overview

Largest Forward Path Sum Across Two Increasing Arrays. Begin at the first entry of either strictly increasing array and move left to right.

Largest Forward Path Sum Across Two Increasing Arrays

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Begin at the first entry of either strictly increasing array and move left to right. At a value present in both arrays,you may switch to the other array at that same value,counting the shared value only once. Continue to the end of the chosen route. Return the maximum sum of visited values modulo 1000000007,after maximizing the true sum. ```cpp class Solution { public: int maxSum(vector<int>& nums1, vector<int>& nums2); }; ``` ### Examples **Example 1:** ```text Input: nums1 = [1,4,9], nums2 = [2,4,8] Output: 15 Explanation: Start with2 in nums2,visit the common4,and switch to9 in nums1:2+4+9=15. ``` **Example 2:** ```text Input: nums1 = [1,3], nums2 = [2,5] Output: 7 Explanation: With no common value,no switch is possible; the second array sums to7. ``` ### Constraints - Each array has length 1 to 100000. - All values lie in `[1,10000000]`. - Both arrays are strictly increasing. ### Contract considerations Strictly increasing arrays prevent duplicate values within one route. ```hint Read the boundary condition Switch only at shared values and count each shared value once. Include arrays with no shared value and compare true sums before the modulus. ``` ### Boundary reminders Strictly increasing arrays prevent duplicate values within one route.

Overview: Largest Forward Path Sum Across Two Increasing Arrays. Begin at the first entry of either strictly increasing array and move left to right.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Begin at the first entry of either strictly increasing array and move left to right. At a value present in both arrays,you may switch to the other array at that same value,counting the shared value only once. Continue to the end of the chosen route. Return the maximum sum of visited values modulo 1000000007,after maximizing the true sum.

class Solution {
public:
    int maxSum(vector<int>& nums1, vector<int>& nums2);
};

Examples

Example 1:

Input: nums1 = [1,4,9], nums2 = [2,4,8]
Output: 15
Explanation: Start with2 in nums2,visit the common4,and switch to9 in nums1:2+4+9=15.

Example 2:

Input: nums1 = [1,3], nums2 = [2,5]
Output: 7
Explanation: With no common value,no switch is possible; the second array sums to7.

Constraints

  • Each array has length 1 to 100000.
  • All values lie in [1,10000000] .
  • Both arrays are strictly increasing.

Contract considerations

Strictly increasing arrays prevent duplicate values within one route.

Boundary reminders

Strictly increasing arrays prevent duplicate values within one route.