Special sequences
Practice
3 (7 votes)
Algorithms
Dynamic programming
2d dynamic programming
C++
Problem
51% Success 591 Attempts 30 Points 3s Time Limit 256MB Memory 1024 KB Max Code
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 \(a_1 < b_1 < a_2 < b_2 < \) and so on, where \(a_1, a_2, a_3, ..\) is a subsequence of array \(A\) and \(b_1, b_2, b_3, ...\) is a subsequence of array \(B\)
- Sequence such that \(b_1 < a_1 < b_2 < a_2 < \) and so on, where \(a_1, a_2, a_3, ..\) is a subsequence of array \(A\) and \(b_1, b_2, b_3, ...\) 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
\(1 \le T \le 5 \\ 1 \le N \le 2 \times 10^3 \\ -10^6 \le A[i] , B[i] \le 10^6\)
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
116 votes
Tags:
ApprovedGeometryMediumOpenTwo dimensional
Points:30
10 votes
Tags:
AlgorithmsDynamic ProgrammingGrammar-VerifiedImplementationMediumTwo dimensional
Points:30
4 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpen
Editorial
Login to unlock the editorial