B. Array merging | CodeForces Solution | Codeforces Round 875 (Div. 2)


You are given two arrays π‘Ž and 𝑏 both of length 𝑛 . You will merge† these arrays forming another array 𝑐 of length 2⋅𝑛 . You have to find the maximum length of a subarray consisting of equal values across all arrays 𝑐 that could be obtained.

B. Array merging

You are given two arrays a and b both of length n.

You will merge† these arrays forming another array c of length 2β‹…n. You have to find the maximum length of a subarray consisting of equal values across all arrays c that could be obtained.

† 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 ≀ t ≀ 104) β€” the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (1 ≀ n ≀ 2β‹…105) β€” the length of the arrays a and b.

The second line of each test case contains n integers a1, a2, …, an (1 ≀ ai ≀ 2β‹…n) β€” the elements of array a.

The third line of each test case contains n integers b1, b2, …, bn (1 ≀ bi ≀ 2β‹…n) β€” the elements of array b.

It is guaranteed that the sum of n across all test cases does not exceed 2β‹…105.

Output

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

Example

Input

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

Output

2
1
2
5

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].

Solution
void solve()
{

    int n;
    cin >> n;

    vi a(n), b(n);
    int mx1 = 0, mx2 = 0, temp = 0;
    mii m1, m2;

    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        if (i == 0 || a[i] == a[i - 1])
            temp++;
        else
            temp = 1;
        mx1 = max(mx1, temp);
        m1[a[i]] = max(m1[a[i]], temp);
    }
    temp = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
        if (i == 0 || b[i] == b[i - 1])
            temp++;
        else
            temp = 1;
        mx2 = max(mx2, temp);
        m2[b[i]] = max(m2[b[i]], temp);
    }
    int ans = 0;
    for (auto it : m1)
    {
        ans = max(ans, it.second + m2[it.first]);
    }

    for (auto it : m2)
    {
        ans = max(ans, it.second + m1[it.first]);
    }

    cout << ans << endl;
}
TagsB. Array mergingCodeForcesC++Codeforces Round 875 (Div. 2)
Raunit Verma-picture
Raunit Verma

Technical Writer at CodingKaro

Share this on
CodingKaro Poster
CodingKaro
4.7

3K+ Downloads

One of its unique features is an automated coding contest reminder, which sets alarms for upcoming coding contests on popular platforms like CodeChef, CodeForces, and LeetCode, without the need for user input. This feature ensures that users never miss a coding contest and can stay updated with the latest coding challenges.

Download CodingKaro
Other Blogs in Codeforces