Alice was recently studying subsequence. She discovered that it is possible to make an array of ‘N’ integers say ‘arr1’, a subsequence of another array of ‘M’ integers, say ‘arr2’, by inserting 0 or more integers into the array ‘arr2’. As she is busy with her studies, she asked you to find the minimum number of integers that need to be inserted into ‘arr2’ to make ‘arr1’ a subsequence of ‘arr2’.
Note :
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}.
Input Format :
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’.
Output Format :
For each test case, print the minimum number of integers that need to be inserted into ‘arr2’ to make ‘arr1’ a subsequence of ‘arr2’.
Constraints :
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.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
2
3 5
1 2 3
3 2 1 4 7
4 6
2 1 7 3
7 1 7 2 3 1
2
1
Test Case 1 :
‘arr1’ = {1, 2, 3} and ‘arr2’ = {3, 2, 1, 4, 7}.
We can insert 2 at the 4th position in ‘arr2’. After insertion, ‘arr2’ = {3, 2, 1, 2, 4, 7}.
We can insert 3 at the 5th position in ‘arr2’. After insertion, ‘arr2’ = {3, 2, 1, 2, 3, 4, 7}.
‘arr1’ is now a subsequence of ‘arr2’. Therefore, the number of insertions needed is 2.
Test Case 2 :
‘arr1’ = {2, 1, 7, 3} and ‘arr2’ = {7, 1, 7, 2, 3, 1}.
We can insert 2 at the 2nh position in ‘arr2’. After insertion, ‘arr2’ = {7, 2, 1, 7, 2, 3, 1}.
‘arr1’ is now a subsequence of ‘arr2’. Therefore, the number of insertions needed is 1.
1
3 7
4 1 2
8 4 9 2 1 2 9
0
Test Case 1 :
‘arr1’ = {4, 1, 2} and ‘arr2’ = {8, 4, 9, 2, 1, 2, 9}.
‘arr1’ is already a subsequence of ‘arr2’. Therefore, we do not insert any integer in ‘arr2’.
Try to insert each integer of ‘arr1’ at every possible position in ‘arr2’.
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):
O(2 ^ (N + M)), where ‘N’ and ‘M’ denote the number elements in ‘arr1’ and ‘arr2’, respectively.
In the worst case, we have to insert all the elements of ‘arr1’ into the ‘arr2’ array. After insertion, the size of ‘arr2’ will be ‘N’ + ‘M’. For any i-th position in ‘arr2’, we have two choices, either we will insert an element at the i-th position, or we will not insert.
Therefore, we have a total of 2 ^ (N + M) different ways to insert elements of ‘arr1’ into the ‘arr2’ array, and for each insertion, we take O(1) time. Hence, the total time complexity will be O(2 ^ (N + M)).
O(N + M), where ‘N’ and ‘M’ denote the number elements in ‘arr1’ and ‘arr2’, respectively.
Our ‘allInsertions’ function requires O(N + M) space in the form of recursive stack space.
Therefore, the total space complexity will be O(N + M).