
Input: ‘N’ = 5, ‘M’ = 3, ‘P’ = {1, 2, 4, 5, 3} ‘Q’ = {2, 1, 3}.
Output: {2, 1, 3, 4, 5}.
Since the array ‘P’ should be sorted such that the relative order of ‘Q’ is maintained so, 2 comes before 1 in ‘P’, then 1 comes before 3 and after 2 and then 3 comes before 1 and 2. Finally, all the remaining elements are appended at last in sorted order.
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains an integer ‘N’ and ‘M’ denoting the length of the array ‘P’ and array ‘Q’.
The second line of each test case contains ‘N’ space-separated integers.
The third line of each test case contains ‘M’ space-separated integers.
For each test case, you don’t need to print anything just rearrange the array ‘P’.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= M <= 10^5
Sum of N Over all the Test cases <= 10^5
Sum of M Over all the Test cases <= 10^5
Time Limit: 1 sec
In this approach, examine the elements of array 2 to see if they appear in array 1. To find the elements, use a binary search. If elements from array 2 are located in array 1, print them. In the end, print all of the remaining elements from array 1.
In this approach, the goal is to keep track of the frequency of each element of the first array in a hash table.