Geeksforgeeks

Error

×

Leaderboard

Showing:

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

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5275 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4411 |

@sanjay05 | 3762 |

@mantu_singh | 3666 |

@gfgaccount | 3605 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 957 |

@kanewilliamson123 | 894 |

@pradeeppatidar1999 | 868 |

@akhileshkumar562002 | 862 |

@wallflowe101 | 826 |

@pd420786 | 813 |

@infilooop | 800 |

@nikhilkushwah280 | 796 |

@bunnybug320 | 796 |

Complete Leaderboard |

Given two integer arrays **A1[ ] **and **A2[ ] **of size **N** and **M** respectively. Sort the first array **A1[ ] **such that all the relative positions of the elements in the first array are the same as the elements in the second array **A2[ ]**.

See example for better understanding.

**Note**: If elements are repeated in the second array, consider their first occurance only.

**Example 1:**

**Input:
**N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {2, 1, 8, 3}
**Output:
**2 2 1 1 8 8 3 5 6 7 9**
Explanation: **Array elements of A1[] are
sorted according to A2[]. So 2 comes first
then 1 comes, then comes 8, then finally 3
comes, now we append remaining elements in
sorted order.

**Example 2:**

**Input:
**N = 11
M = 4
A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
A2[] = {99, 22, 444, 56}
**Output:
**1 1 2 2 3 5 6 7 8 8 9**
Explanation: **No A1[] elements are in A2[]
so we cannot sort A1[] according to A2[].
Hence we sort the elements in non-decreasing
order.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **sortA1ByA2()** which takes the array **A1[ ]**, array **A2[ ]** and their respective size **N** and **M** as input parameters and returns the sorted array **A1[ ] **such that the relative positions of the elements in **A1[ ]** are same as the elements in **A2[ ]**. For the elements not present in **A2[ ]** but in **A1[ ]**, it appends them at the last in **increasing **order.

**Expected Time Complexity:** O(N * Log(N)).

**Expected Auxiliary Space:** O(N).

**Constraints:**

1 ≤ N, M ≤ 10^{6}

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

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?

YesLoading...

Sort an array according to the other

...