0% completed
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
are placed inarr1
first in the same order as inarr2
. The remaining elements 2 and 3 are placed at the end in ascending order.
Example 2:
- Input: arr1 = [8, 3, 9, 1, 7, 5], arr2 = [3, 9, 8]
- Expected Output: [3, 9, 8, 1, 5, 7]
- Justification: Elements 3, 9 and 8 from
arr2
are placed inarr1
first in the same order as inarr2
. Remaining elements 1, 5 and 7 are placed at the end in ascending order.
Example 3:
- Input: arr1 = [10, 10, 7, 10, 7, 9], arr2 = [10, 7]
- Expected Output: [10, 10, 10, 7, 7, 9]
- Justification: Elements 10 and 7 from
arr2
are placed inarr1
first in the same order as inarr2
. The remaining element 9 is placed at the end in ascending order.
Constraints:
- 1 <= arr1.length, arr2.length <= 1000
- 0 <= arr1[i], arr2[i] <= 1000
- All the elements of
arr2
are distinct. - Each
arr2[i]
is inarr1
.
Solution
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.
Step-by-Step Algorithm
-
Find Maximum Element:
- Identify the largest element in arr1 to determine the size of the count array.
-
Initialize Count Array:
- Create an array
count
of sizemaxElement + 1
to store the frequency of each element in arr1. - The size is
maxElement + 1
to accommodate the maximum element's index.
- Create an array
-
Calculate the Frequency of each Element in arr1 Array:
- Iterate through each element in arr1.
- For each element, increment the corresponding index in the count array by 1.
- This step counts the occurrences of each element in arr1.
-
Create Result List:
- Initialize an empty list
result
to store the sorted elements.
- Initialize an empty list
-
Add Elements from arr2 to Result:
- Iterate through each element in arr2.
- For each element, append it to the result list as many times as it appears in the count array.
- Decrement the corresponding index in the count array by 1 for each appended element.
- This ensures that elements from arr2 appear in the result in the specified order.
-
Add Remaining Elements to Result:
- Iterate through the count array from index 0 to
maxElement
. - For each index with a positive count, append the index to the result list as many times as its count.
- Decrement the count for each appended element.
- This step ensures that remaining elements not in arr2 are appended in ascending order.
- Iterate through the count array from index 0 to
-
Convert Result List to Array:
- Convert the result list to an array and return it.
Algorithm Walkthrough
- Input:
- arr1 = [3, 5, 2, 1, 6, 4, 5, 6]
- arr2 = [5, 6, 4, 1]
-
Find Maximum Element:
maxElement
is 6.
-
Initialize Count Array:
count
array is initialized to [0, 0, 0, 0, 0, 0, 0].
-
Populate Count Array:
- Iterate through arr1:
- For 3:
count
becomes [0, 0, 0, 1, 0, 0, 0]. - For 5:
count
becomes [0, 0, 0, 1, 0, 1, 0]. - For 2:
count
becomes [0, 0, 1, 1, 0, 1, 0]. - For 1:
count
becomes [0, 1, 1, 1, 0, 1, 0]. - For 6:
count
becomes [0, 1, 1, 1, 0, 1, 1]. - For 4:
count
becomes [0, 1, 1, 1, 1, 1, 1]. - For 5:
count
becomes [0, 1, 1, 1, 1, 2, 1]. - For 6:
count
becomes [0, 1, 1, 1, 1, 2, 2].
- For 3:
- Iterate through arr1:
-
Create Result List:
- Initialize
result
as an empty list.
- Initialize
-
Add Elements from arr2 to Result:
- Iterate through arr2:
- For 5: Append 5,
count
becomes [0, 1, 1, 1, 1, 1, 2],result
becomes [5]. - For 5: Append 5,
count
becomes [0, 1, 1, 1, 1, 0, 2],result
becomes [5, 5]. - For 6: Append 6,
count
becomes [0, 1, 1, 1, 1, 0, 1],result
becomes [5, 5, 6]. - For 6: Append 6,
count
becomes [0, 1, 1, 1, 1, 0, 0],result
becomes [5, 5, 6, 6]. - For 4: Append 4,
count
becomes [0, 1, 1, 1, 0, 0, 0],result
becomes [5, 5, 6, 6, 4]. - For 1: Append 1,
count
becomes [0, 0, 1, 1, 0, 0, 0],result
becomes [5, 5, 6, 6, 4, 1].
- For 5: Append 5,
- Iterate through arr2:
-
Add Remaining Elements to Result:
- Iterate through the count array:
- For 2: Append 2,
count
becomes [0, 0, 0, 1, 0, 0, 0],result
becomes [5, 5, 6, 6, 4, 1, 2]. - For 3: Append 3,
count
becomes [0, 0, 0, 0, 0, 0, 0],result
becomes [5, 5, 6, 6, 4, 1, 2, 3].
- For 2: Append 2,
- Iterate through the count array:
-
Convert Result List to Array:
result
list is converted to array [5, 5, 6, 6, 4, 1, 2, 3].
Code
Complexity Analysis
Time Complexity: O(n + m + k)
n:
Length ofarr1
.m:
Length ofarr2
.k:
Maximum element inarr1
.- We iterate through arr1 to populate the count array (O(n)). Then, we iterate through
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).
Space Complexity: O(k)
k:
Maximum element inarr1
.- The space complexity is determined by the size of the count array, which is proportional to the maximum element in
arr1
. Thus, the space complexity is O(k).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible