
Given a set of ‘N’ nuts of different sizes and ‘N’ bolts of different sizes. There is a one-one mapping between nuts and bolts. Your task is to find the correct match of nuts and bolts from the given set and assign a nut to a bolt when it is matched.
Note:1. A comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with a nut to see which one is bigger/smaller.
2. The set of Nuts/Bolts will consist of numeric strings that represent their sizes. The size of each nut/bolt will be equal to its numerical value. For example, the size of ‘10’ will be equal to 10.
3. For every bolt, there will exist a nut of the same size.
4. Don’t return or print anything. Modify the original sequence of nuts and bolts such that each nut of a particular size matches its bolt of the same size.
5. Store the matched pair of nuts and bolts in any order.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘3*T’ lines represent the ‘T’ test cases.
The first line of each test case contains an integer ‘N’ denoting the number of nuts/bolts.
The second line of each test case contains ‘N’ space-separated strings denoting the nuts of different sizes.
The third line of each test case contains ‘N’ space-separated strings denoting bolts of different sizes.
Output Format:
For each test case, 2 arrays will be printed on separate lines such that the first array contains the nuts, the second array contains the bolts and the nut present at the ‘ith’ position in the array of nuts should be a perfect match for the bolt present at the ‘ith’ position in the array of bolts.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5 * 10^3
1 <= SIZE <= 10^9
Time limit: 1 second
1
5
2 8 3 4 6
6 4 3 8 2
2 3 4 6 8
2 3 4 6 8
In the output sequence, we need to match each nut of a particular size to a bolt of the same size.
We can see clearly that the nut of size 2 which was initially at index 0 ( 0 based indexing ) in the nuts sequence matches with a bolt of size 2 which is at index 4 in the bolts sequence.
Similarly, the nut of size 8 clearly matches with a bolt of size 8.
The nut of size 3 matches with the bolt of size 3.
The nut of size 4 matches with the bolt of size 4.
And, the nut of size 6 matches with the bolt of size 6.
2
7
12 14 26 17 11 15 23
11 23 15 17 26 14 12
5
12 10 4 6 3
10 4 3 6 12
11 12 14 15 17 23 26
11 12 14 15 17 23 26
3 4 6 10 12
3 4 6 10 12
Can we sort the two arrays using each other?
The idea is to quick sort the two arrays using each other.
Return the index of the pivot in the nuts array. Call it ‘i’.
3. Now, using NUTS[i] as a pivot in the bolts array, partition the bolts array.
4. Recur for [LOW……PIVOT-1] and [PIVOT+1…….HIGH] for nuts and bolts array.
5. Lastly, we get the required pairs.
O(N * log(N)), where ‘N’ is the number of nuts/bolts.
Partitioning operations can easily be implemented in O(N). These partition operations are applied on the left and right sub-array of nuts and bolts.
O(1), as it uses constant space.