Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.
The sort-and-search variant (#Method 3 - Sort-and-Search (binary-search + visited)) is educational and useful when you already need a sorted snapshot of arr1, but it’s rarely the best practical choice for this problem.
Because 0 <= arr1[i], arr2[i] <= 1000, we use an array to count every element, lets call it cnt.
Now, we don’t need arr1, we can actually fill the sorted array in place. So, we will go through arr2, from left to right. When, we find frequency of it in cnt, we fill it in arr1.
Now, we fill in remaining elements in cnt, which are not yet encountered.
#include<vector>usingnamespace std;
classSolution {
public: vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
vector<int> cnt(1001, 0);
for (int num : arr1) cnt[num]++;
int i =0;
for (int num : arr2) {
while (cnt[num]-->0) arr1[i++] = num;
}
for (int num =0; num < (int)cnt.size(); ++num) {
while (cnt[num]-->0) arr1[i++] = num;
}
return arr1;
}
};
classSolution {
publicint[]relativeSortArray(int[] arr1, int[] arr2) {
int[] cnt =newint[1001];
for(int num : arr1) {
cnt[num]++;
}
int i = 0;
for(int num : arr2) {
while(cnt[num]--> 0) {
arr1[i++]= num;
}
}
for(int num = 0; num < cnt.length; num++) {
while(cnt[num]--> 0) {
arr1[i++]= num;
}
}
return arr1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defrelativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
cnt = [0] *1001for num in arr1:
cnt[num] +=1 res = []
for num in arr2:
while cnt[num] >0:
res.append(num)
cnt[num] -=1for num in range(len(cnt)):
while cnt[num] >0:
res.append(num)
cnt[num] -=1return res
If you prefer working with a sorted auxiliary array, another approach is to sort a copy of arr1, then for every element in arr2 binary-search the first occurrence and copy its duplicates into the output while marking them visited; finally append the leftover (not-visited) elements in sorted order. This is mostly an educational variant — it leverages binary search for locating the starting index of each value and a visited[] array to avoid duplicating elements.
Copy arr1 into an auxiliary array aux and sort it.
Create a boolean visited array initialized to false.
For each value x in arr2:
Binary-search aux for the first index idx where aux[idx] == x.
If found, walk forward from idx, append each equal x to the output and mark the positions visited.
After processing all arr2, append all elements in aux whose visited flag is false (they are the not-present elements), which are already in sorted order.
This approach is simple and good for teaching binary search and careful in-place bookkeeping. It’s not asymptotically better than the map+sort method, and counting-sort is preferable when the value range is small.
from typing import List
import bisect
classSolution:
defrelativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
aux = sorted(arr1)
n = len(aux)
vis = [False] * n
out = []
for x in arr2:
i = bisect.bisect_left(aux, x)
if i == n or aux[i] != x:
continuewhile i < n and aux[i] == x:
ifnot vis[i]:
out.append(x)
vis[i] =True i +=1for i in range(n):
ifnot vis[i]:
out.append(aux[i])
return out