Array merging (Codeforces 1831B)
Tags: Greedy, constructive algorithms

You are given two arrays $$$a$$$ and $$$b$$$ both of length $$$n$$$.You will merge$$$^\dagger$$$ these arrays forming another array $$$c$$$ of length $$$2 \cdot n$$$. You have to find the maximum length of a subarray consisting of equal values across all arrays $$$c$$$ that could be obtained.$$$^\dagger$$$ A merge of two arrays results in an array $$$c$$$ composed by successively taking the first element of either array (as long as that array is nonempty) and removing it. After this step, the element is appended to the back of $$$c$$$. We repeat this operation as long as we can (i.e. at least one array is nonempty).

Input: Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$ and $$$b$$$.The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 2 \cdot n$$$) — the elements of array $$$a$$$.The third line of each test case contains $$$n$$$ integers $$$b_1,b_2,\ldots,b_n$$$ ($$$1 \le b_i \le 2 \cdot n$$$) — the elements of array $$$b$$$.It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case, output the maximum length of a subarray consisting of equal values across all merges.

Note: In the first test case, we can only make $$$c=[2,2]$$$, thus the answer is $$$2$$$.In the second test case, since all values are distinct, the answer must be $$$1$$$.In the third test case, the arrays $$$c$$$ we can make are $$$[1,2,1,2]$$$, $$$[1,2,2,1]$$$, $$$[2,1,1,2]$$$, $$$[2,1,2,1]$$$. We can see that the answer is $$$2$$$ when we choose $$$c=[1,2,2,1]$$$. 

Sample Inputs:
4
1
2
2
3
1 2 3
4 5 6
2
1 2
2 1
5
1 2 2 2 2
2 1 1 1 1

Sample Outputs:
2
1
2
5

Solution (Python):
import sys

def solve():
    inp = sys.stdin.readline
    n = int(inp())
    a = [0] * (2 * n + 1)
    p = -1
    for i in map(int, inp().split()):
        if i != p:
            p = i
            c = 1
        else:
            c += 1
        a[i] = max(a[i], c)
    res = max(a)
    p = -1
    for i in map(int, inp().split()):
        if i != p:
            p = i
            c = 1
        else:
            c += 1
        res = max(res, a[i] + c)
    print(res)

def main():
    for i in range(int(sys.stdin.readline())):
        solve()
if __name__ == '__main__':
    main()

Solution (Python):
def max_in_row(a, d):
    prev = a[0]
    count = 1
    d[prev] = count
    for i in range(1, len(a)):
        if a[i] == prev:
            count += 1
            if d[a[i]] < count:
                d[a[i]] = count
        else:
            count = 1
            if d[a[i]] < count:
                d[a[i]] = count
        prev = a[i]

def max_sum(a, b, da, db):
    max_in_row(a, da)
    max_in_row(b, db)
    mx = 0
    temp = 0
    for i in range(len(da)):
        temp = da[i] + db[i]
        if temp > mx:
            mx = temp
    return mx
t = int(input())
anses = []
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    da = [0] * (2 * n + 1)
    db = [0] * (2 * n + 1)
    ans = max_sum(a, b, da, db)
    anses.append(ans)
for ans in anses:
    print(ans)

Solution (Python):
def subarray(arr, n2):
    temp = 1
    prev_value = arr[0]
    dict_temp = {prev_value: 1}
    for i in range(1, n2):
        prev_value = arr[i - 1]
        if arr[i] == prev_value:
            temp = temp + 1
        else:
            if prev_value not in dict_temp:
                dict_temp[prev_value] = temp
            else:
                dict_temp[prev_value] = max(temp, dict_temp[prev_value])
            temp = 1
    if arr[n2 - 1] not in dict_temp:
        dict_temp[arr[n2 - 1]] = temp
    else:
        dict_temp[arr[n2 - 1]] = max(temp, dict_temp[arr[n2 - 1]])
    return dict_temp
for test_case in range(int(input())):
    n = int(input())
    arr1 = list(map(int, input().split()))
    arr2 = list(map(int, input().split()))
    dict1 = subarray(arr1, n)
    dict2 = subarray(arr2, n)
    ans = max(max(dict1.values()), max(dict2.values()))
    for key in dict1.keys():
        if key in dict2:
            summ = dict1[key] + dict2[key]
            if summ > ans:
                ans = summ
    print(ans)