0% completed
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.
arr2 are placed in arr1 first in the same order as in arr2. The remaining elements 2 and 3 are placed at the end in ascending order.arr2 are placed in arr1 first in the same order as in arr2. Remaining elements 1, 5 and 7 are placed at the end in ascending order.arr2 are placed in arr1 first in the same order as in arr2. The remaining element 9 is placed at the end in ascending order.Constraints:
arr2 are distinct.arr2[i] is in arr1.To solve this problem, we use a counting sort approach to arrange the elements of arr1 based on the order defined by arr2. First, we count the occurrences of each element in arr1 and store these counts in an array. Then, we use the counts to construct the sorted result by placing elements from arr2 in the specified order, followed by the remaining elements in ascending order.
This approach is effective because it leverages counting sort's efficiency for sorting integers when the range of values is not excessively large. By focusing on the order defined by arr2, we ensure that the relative ordering is maintained, and by appending the remaining elements in ascending order, we satisfy the problem's requirements.
Find Maximum Element:
Initialize Count Array:
count of size maxElement + 1 to store the frequency of each element in arr1.maxElement + 1 to accommodate the maximum element's index.Calculate the Frequency of each Element in arr1 Array:
Create Result List:
result to store the sorted elements.Add Elements from arr2 to Result:
Add Remaining Elements to Result:
maxElement.Convert Result List to Array:
Find Maximum Element:
maxElement is 6.Initialize Count Array:
count array is initialized to [0, 0, 0, 0, 0, 0, 0].Populate Count Array:
count becomes [0, 0, 0, 1, 0, 0, 0].count becomes [0, 0, 0, 1, 0, 1, 0].count becomes [0, 0, 1, 1, 0, 1, 0].count becomes [0, 1, 1, 1, 0, 1, 0].count becomes [0, 1, 1, 1, 0, 1, 1].count becomes [0, 1, 1, 1, 1, 1, 1].count becomes [0, 1, 1, 1, 1, 2, 1].count becomes [0, 1, 1, 1, 1, 2, 2].Create Result List:
result as an empty list.Add Elements from arr2 to Result:
count becomes [0, 1, 1, 1, 1, 1, 2], result becomes [5].count becomes [0, 1, 1, 1, 1, 0, 2], result becomes [5, 5].count becomes [0, 1, 1, 1, 1, 0, 1], result becomes [5, 5, 6].count becomes [0, 1, 1, 1, 1, 0, 0], result becomes [5, 5, 6, 6].count becomes [0, 1, 1, 1, 0, 0, 0], result becomes [5, 5, 6, 6, 4].count becomes [0, 0, 1, 1, 0, 0, 0], result becomes [5, 5, 6, 6, 4, 1].Add Remaining Elements to Result:
count becomes [0, 0, 0, 1, 0, 0, 0], result becomes [5, 5, 6, 6, 4, 1, 2].count becomes [0, 0, 0, 0, 0, 0, 0], result becomes [5, 5, 6, 6, 4, 1, 2, 3].Convert Result List to Array:
result list is converted to array [5, 5, 6, 6, 4, 1, 2, 3].n: Length of arr1.m: Length of arr2.k: Maximum element in arr1.arr2 to construct part of the result (O(m)). Finally, we iterate through the count array to add remaining elements in ascending order (O(k)). Hence, the total time complexity is O(n + m + k).k: Maximum element in arr1.arr1. Thus, the space complexity is O(k)......
.....
.....