Back to course home
0% completed
Solution: Relative Sort Array
Problem Statement
Given two arrays arr1
of length n
and arr2
of length m
, sort the elements of arr1
such that the relative ordering of items in arr1
is the same as in arr2
. Elements that do not appear in arr2
should be placed at the end of arr1
in ascending order.
It is given that elements of arr2
are distinct, and all elements in arr2
are also in arr1
.
Examples
Example 1:
- Input: arr1 = [3, 5, 2, 1, 6, 4, 5, 6], arr2 = [5, 6, 4, 1]
- Expected Output: [5, 5, 6, 6, 4, 1, 2, 3]
- Justification: Elements 5, 6, 4 and 1 from `arr2
.....
.....
.....
Like the course? Get enrolled and start learning!