

1) ‘arr1’ consists of distinct positive integers, and ‘arr2’ can contain duplicates.
2) You are allowed to insert integers at any position in ‘arr2’. For example, if ‘arr2’ = {1, 2, 3}, you can insert 4 at 2nd position to make it {1, 4, 2, 3}.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, where ‘N’ and ‘M’ denote the total number of elements in ‘arr1’ and ‘arr2’, respectively.
The second line of each test case contains ‘N’ space-separated integers, denoting the elements of ‘arr1’.
The second line of each test case contains ‘M’ space-separated integers, denoting the elements of ‘arr2’.
For each test case, print the minimum number of integers that need to be inserted into ‘arr2’ to make ‘arr1’ a subsequence of ‘arr2’.
1 <= T <= 5
1 <= N, M <= 1000
1 <= arr1[i], arr2[i] <= 10 ^ 9
Where ‘T’ is the number of test cases, ‘N’, ‘M’ denotes the number of elements in ‘arr1’ and ‘arr2’, respectively, and ‘arr1[i]’, ‘arr2[i]’ denotes the i-th element in ‘arr1’ and ‘arr2’, respectively.
You do not need to print anything, it has already been taken care of. Just implement the given function.
The idea here is to insert every integer of ‘arr1’ at every possible position in the ‘arr2’. After inserting all the elements of ‘arr1’ in ‘arr2’, ‘arr1’ will become a subsequence of ‘arr2’. Out of all such series of insertions, we will take the one with the minimum number of insertions.
This function is used to insert all the elements of ‘arr1’ from ‘low1’ to ‘N’ - 1 inclusive into every possible position from ‘low2’ to ‘M’ - 1 inclusive, in ‘arr2’.
This function will take six parameters :
int allInsertions(arr1, arr2, low1, low2, N, M):
The idea here is to optimize the previous approach using dynamic programming. We are calling some functions, again and again, so we will call them once and store their values in a ‘dp’ array.
The below diagram shows that ‘allInsertions’ are called multiple times for ‘arr1’ = {1, 2, 3} and ‘arr2’ = {2, 1, 3}, hence there are overlapping subproblems.
This function is used to insert all the elements of ‘arr1’ from ‘low1’ to ‘N’ - 1 inclusive, into every possible position from ‘low2’ to ‘M’ - 1 inclusive, in ‘arr2’.
This function will take seven parameters :
int allInsertions(arr1, arr2, low1, low2, N, M, dp):
The idea here is to use the fact that ‘arr1’ contains all distinct elements. We will do a one-to-one mapping between all the elements of ‘arr2’ and ‘arr1’ in such a way that for any i-th element of ‘arr2’ if ‘arr2[i]’ is not present in ‘arr1’ we will simply ignore this element, otherwise, we will map ‘arr2[i]’ with the index of ‘arr2[i]’ in ‘arr1’ and store this index into an array of integers, say ‘mappingIndex’. Now, we will find the length of the longest increasing subsequence of ‘mappingIndex’. The length of the longest increasing subsequence of ‘mappingIndex’ will basically be the length of the longest common subsequence between ‘arr1’ and ‘arr2’. Therefore, we need to insert (‘N’ - length of the longest common subsequence between ‘arr1’ and ‘arr2’) number of elements in ‘arr2’ to make ‘arr1’ a subsequence.
The idea here is to use the fact that ‘arr1’ contains all distinct elements. We will do a one-to-many mapping between all the elements of ‘arr1’ to its corresponding elements in ‘arr2’. We will create an array of integers, say ‘dp’ of size ‘M’, where ‘dp[i]’ will denote the length of the longest common subsequence between ‘arr1’ and ‘arr2[0 . . . i ]’, and ending at ‘i’. Now, for any i-th element in ‘arr1’, we will iterate through all its corresponding elements in ‘arr2’ and update ‘dp’ accordingly.