Skip to content

1122. Relative Sort Array #217

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

The problem requires sorting an array arr1 such that its elements are rearranged to match the relative order of elements found in another array arr2. Elements from arr2 should appear in the same order in arr1, and any remaining elements from arr1 that are not present in arr2 should be placed at the end in ascending order.

Key Points:

  • Elements of arr2 are distinct.
  • All elements in arr2 are guaranteed to be present in arr1.
  • Any element in arr1 that is not in arr2 should appear at the end, sorted in ascending order.

Approach:

  1. Use a Hashmap: The order of arr2 is essential. By storing the index of each element in arr2, we can sort arr1 based on the relative order of arr2. We will use the v…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jan 6, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jan 6, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants