Special sequences

3

7 votes
Problem

You are given an array A of length N and array B of length M.

A special sequence can be defined in either of the two ways:

  • Sequence such that a1<b1<a2<b2< and so on, where a1,a2,a3,.. is a subsequence of array A and b1,b2,b3,... is a subsequence of array B
  • Sequence such that b1<a1<b2<a2< and so on, where a1,a2,a3,.. is a subsequence of array A and b1,b2,b3,... is a subsequence of array B

Find the maximum length of a special sequence that can be formed.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains two space-separated integers N and M.
  • The second line of each test case contains N space-separated integers denoting array A.
  • The third line of each test case contains M space-separated integers denoting array B.

Output format

For each test case, print the maximum length of a special sequence in a new line.

Constraints

1T51N2×103106A[i],B[i]106

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

For Test Case 1 :-

  • Choose subsequence 1,3 from array A
  • Choose subsequence 2 from array B
  • Special sequence will be 1<2<3 .

For Test Case 2 :-

  • Since all the elements in array A and B are same. 
  • Choose any element and that will be a special sequence.
Editor Image

?