Relative Sort Array
Practice this coding question on Coding Gurukul.
Problem statement
A warehouse sorts stock codes according to a priority list supplied by its manager. Codes on that list come first in the manager's order; codes outside the list are placed afterward in ordinary numeric order. Your task Reorder arr1 so that values present in arr2 follow arr2's ordering. Preserve all copies of each value. Sort values not present in arr2 in increasing order after the prioritized values. Important details The priority list contains distinct values, and every priority value appears in arr1. The priority list is an ordering rule, not a second batch to append. Negative values are not used in this stock-code format. Worked example For arr1 = [4, 2, 4, 3, 1] and arr2 = [4, 1], place both 4s first, then 1, then the remaining codes 2 and 3. Print 4 4 1 2 3.
Input format
First line: n m. Next n integers: arr1. Next m integers: arr2.
Output format
Print the resulting values in order, separated by spaces. Print EMPTY if there are no values.
Constraints
1 <= m <= n <= 1000; 0 <= values <= 1000; arr2 has distinct values present in arr1.
Sample input
5 2 4 2 4 3 1 4 1
Sample output
4 4 1 2 3
Sign in to run or submit a solution in C++, Java or Python.