


Let ‘ARR1’ = [1, 5, 10, 15, 20] and ‘ARR2’ = [2, 4, 5, 9, 15]
In this example, common points are 5, 15.
First, we start with ARR2 and take the sum till 5 (i.e. sum = 11). Then we will switch to ‘ARR1’ at element 10 and take the sum till 15. So sum = 36. Now no element is left in ‘ARR2’ after 15, so we will continue in array 1. Hence sum is 56. And the path is 2 -> 4 -> 5 -> 10 -> 15 -> 20.

The first line of input contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M,’ representing the number of elements in ‘ARR1’ and ‘ARR2’.
The second line of each test case contains ‘N’ single space-separated integers denoting the values of ‘ARR1’.
The third line of each test case contains ‘M’ single space-separated integers denoting the values of ‘ARR2’.
For each test case, print the maximum sum value.
Print the output of each test case in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 100
1 <= ‘N’, ’M’ <= 10^4
1 <= ‘ARR1[i]’, ‘ARR2[j]’ <= 10^5
Time Limit: 1 second
In this problem, our primary focus is on the common elements i.e. an element that is present in both the arrays. Then, we have to decide whether we have to make a switch. So for that, first we store all the elements of ‘ARR1’ and ‘ARR2’ into ‘MAP1’ and ‘MAP2’ respectively.
Now we call our ‘maximiseSumHelper’ function. We call this function for both cases i.e starting with ‘ARR1’ and starting with ‘ARR2’. Then we return the maximum of the two cases. During the recursive call, if we find a common element that is present in both the arrays, then we have two options at that point i.e. either we can switch to the other array or ontinue with the same array.
Here is the algorithm :
maximiseSumHelper(‘ARR1’, ‘ARR2’, ‘currArr’, ‘currInd’) function works as follows:
In our previous approach, we use a recursive solution for solving this problem. But there are a lot of repetitive calculations in our previous approach. Let's assume a case in which both the arrays contain the same values, say ‘ARR1’ = [1, 2, 3, 4] and ‘ARR2’ = [1, 2, 3, 4].
So in this case at every index, we have two choices i.e either we can switch to the other array or we can continue with the same array. So here we can use memoization to get rid of repetitive calls.
Here is the algorithm :
This approach is similar to the previous approach with some additions. In this approach, we use the ‘memo’ array.
Here ‘memo[0][j]’ stores the maximum sum till now when ‘ARR1’ has ‘i’ elements.
And ‘memo[1][j]’ stores the maximum sum till now when ‘ARR2’ has ‘j’ elements.
maximiseSumHelper(‘ARR1’, ‘ARR2’, ‘currArr’, ‘currInd’, ’memo’) function is the same as described in the previous approach with some additions:
In this problem, our primary focus is on the common elements, i.e., elements present in both the arrays. Then, we have to decide whether we have to make a switch. So for that, we take two variables‘ currSumArr1’ and ‘currSumArr2’ in which we store the sum of all the elements between the current common element and the last common element of ‘ARR1’ and ‘ARR2’, respectively. And when we encounter a common element, we compare the accumulated sum in both paths, and we pick the bigger one. Then add this into ‘maximumSum.’
Here is the algorithm :