Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 786 |

@arpitsatnalika | 777 |

@tujhekya | 602 |

@prakharxyz | 601 |

@code_it_ | 591 |

@iamronak | 505 |

@imnm | 500 |

@pradeepkumarjad | 488 |

@satyam0460 | 481 |

@nitesha125 | 476 |

@Rahul Jadhav 1 | 466 |

Complete Leaderboard |

Given two arrays **A1[]** and **A2[]** of size **N** and **M** respectively. The task is to sort A1 in such a way that the relative order among the elements will be same as those in A2. For the elements not present in A2, append them at last in sorted order. It is also given that the number of elements in A2[] are smaller than or equal to number of elements in A1[] and A2[] has all distinct elements.

**Note:** Expected time complexity is **O(N log(N))**.

**Input:**

First line of input contains number of testcases. For each testcase, first line of input contains length of arrays N and M and next two line contains N and M elements respectively.

**Output:**

Print the relatively sorted array.

**Constraints:**

1 ≤ T ≤ 100

1 ≤ N,M ≤ 10^{6}

1 ≤ A1[], A2[] <= 10^{6}

**Example:**

**Input:**

2

11 4

2 1 2 5 7 1 9 3 6 8 8

2 1 8 3

8 4

2 6 7 5 2 6 8 4

2 6 4 5

**Output:**

2 2 1 1 8 8 3 5 6 7 9

2 2 6 6 4 5 7 8

**Explanation:
Testcase 1:** After sorting the resulted output is 2 2 1 1 8 8 3 5 6 7 9.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Relative Sorting

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.