# Relative Sorting

Moderate
0/80
Average time to solve is 25m

## Problem statement

Given two arrays ‘ARR’ and ‘BRR’ of size ‘N’ and ‘M’ respectively. Your task is to sort the elements of ‘ARR’ in such a way that the relative order among the elements will be the same as those are in ‘BRR’. For the elements not present in ‘BRR’, append them in the last in sorted order.

For example

``````Consider the arrays as ARR = { 9, 5, 8, 4, 6, 5 } and BRR = { 8, 4, 5 }
The output for the above example  is { 8, 4, 5, 5, 6, 9 }.
``````

Note:

``````Elements of ‘BRR’ are non repeating.
``````
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
``````1 <= T <= 5
1 <= N <= 10 ^ 5
1 <= M <= 10 ^ 5
(-10 ^ 9) <= ARR[i] , BRR[i]  <= (10 ^ 9)

Time limit: 1 sec
``````
##### Sample Input 1:
``````2
6 3
9 5 8 4 6 5
8 4 5
4 4
1 8 1 6
1 9 6 7
``````
##### Sample Output 1:
``````8 4 5 5 6 9
1 1 6 8
``````

#### Explanation:

``````For test case 1:
As 8 comes first in BRR, so we first add all occurrences of 8 in ARR, in our resultant array RES. Now, RES={8} and ARR={9,5,4,6,5}.
After 8, 4 comes in BRR, so we add all occurrences of 4 that are in ARR, in the RES array. Now, RES={8,4} and ARR={9,5,6,5}.
After 4, 5 comes in BRR, so we add all occurrences of 5 that are in ARR, in the RES array. Now, RES={8,4,5,5} and ARR={9,6}.
Now, after processing all the elements of BRR, we will add the remaining elements of ARR in sorted order, in our RES array.
So the final RES is {8,4,5,5,6,9}.

For test case 2:
As 1 comes first in BRR, so we first add all occurrences of 1 in ARR, in our resultant array RES. Now, RES={1,1} and ARR={8,6}.
After 1, 9 comes in BRR, so we add all occurrences of 9 that are in ARR, in the RES array, but 9 is not present in ARR, so RES remains the same. Now, RES={1,1} and ARR={8,6}.
After 9, 6 comes in BRR, so we add all occurrences of 6 that are in ARR, in the RES array. Now, RES={1,1,6} and ARR={8}.
After 9, 7 comes in BRR, so we add all occurrences of 7 in ARR, in our RES, but 7 is not present in ARR, so RES remains the same. Now, RES={1,1,6} and ARR={8}.
Now, after processing all the elements of BRR, we will add the remaining elements of ARR in sorted order, in our RES array.
So the final RES is {1,1,6,8}.
``````
##### Sample Input 2:
``````2
3 5
7 5 8
8 4 5 6 3
4 2
9 2 2 4
5 2
``````
##### Sample Output 2:
``````8 5 7
2 2 4 9
``````
Console