Given two arrays A1[] and A2[] of size N and M respectively. The task is to sort A1 in such a way that the relative order among the elements will be same as those in A2. For the elements not present in A2, append them at last in sorted order. It is also given that the number of elements in A2[] are smaller than or equal to number of elements in A1[] and A2[] has all distinct elements.
_______________________________________________________________________
t = int(input())
for i in range(t):
n,m = list(map(int,input().split()))
arr1 = list(map(int,input().split()))
arr2 = list(map(int,input().split()))
# 0 1 2 3 4 5 6 7 8 9 10
# 2 1 2 5 7 1 9 3 6 8 8
# 2 1 8 3
result=[]
for i in range(m):
for j in range(n):
if(arr1[j]==arr2[i]):
result.append(arr1[j])
new=[]
for i in arr1:
if(i not in result):
new.append(i)
new = sorted(new)
print(*result,*new)
Comments
Post a Comment