You are given a positive integer ‘K’ and two arrays ‘arr1’ and ‘arr2’ of the length ‘N’ and ‘M’ respectively. Both the given arrays only contain integers from ‘0’ to ‘9’.
An operation is defined as follows.
Select any subsequence from ‘arr1’ and ‘arr2’ say ‘S1’ and ‘S2’, of any length.
Make a new array by merging ‘S1’ and ‘S2’ while keeping relative order of elements in ‘S1’ and ‘S2’.
You need to return the resulting array after doing the above operation in a way such that, size of the resulting array is ‘K’ and it is the lexicography maximum among all such possible arrays.
Note :
Array ‘X’ of size ‘K’ is lexicographically larger than array ‘Y’ of size ‘K’, if for some ‘j’ < ‘K’ ‘Xi’ = ‘Yi’ for all ‘i’ < ‘j’ and ‘Xj’ > ‘Yj’.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Input Format :
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains three space-separated integers ‘N’, ‘M’ denoting the size of the first array and second array respectively, and ‘K’ denoting the size of the lexicographically greatest array needed.
The second line of each test case contains ‘N’ space-separated integers, representing the elements of the first array, ‘arr1’.
The last line of the test cases contains ‘M’ space-separated integers, representing the elements of the second array, ‘arr2’.
Output Format :
For each test case, print ‘K’ space-separated integers denoting the elements of the lexicographically maximum possible array.
The output of each test case will be printed in a separate line.
Note :
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= N, M <= 50
1 <= K <= N + M
0 <= arr1[i], arr2[i] <= 9
Where ‘T’ is the total number of test cases, ‘N’ and ‘M ’are the size of the given first and second array, ‘K’ is the size of the array that is to be returned, and ‘arr1[i]’, ‘arr2[i]’ represents the ‘i-th’ elements in the first and second array respectively.
Time Limit : 1 sec
2
4 5 4
2 1 6 5
2 2 8 9 1
2 2 2
4 4
5 3
9 6 5 1
5 4
Test case 1 :
We choose a subsequence [ 2, 6, 5] from the first array and subsequence [9] from the second array and merge them as [9, 2, 6, 5] to get the lexicographically maximum possible array of size 4.
Test case 2 :
Select subsequence [4] from the first array and [5] from the second array and merge them to get [5, 4] which is lexicographically maximum possible.
1
2 3 3
4 1
9 2 3
9 4 3
Test case 1:
Select subsequence [4] from the first array and [9, 3] from the second array and merge them as [9, 4, 3] which is lexicographically maximum possible
Consider subsequences of all sizes.
The resulting array may have ‘i’ elements from ‘arr1’ and ‘K’ - ‘i’ elements from ‘arr2’, where ‘i’ can be any integer from ‘0’ to min(‘K’, size of ‘arr1’). So we can iterate through all these ‘i’ and find a resultant array which is lexicographically maximum. First thing to observe is that if we choose a subsequence ‘X’ of length ‘K1’ from ‘arr1’ and subsequence ‘Y’ of length ‘K2’ form ‘arr2’ such that ‘K1’ + ‘K2’ = ‘K’, and after merging ‘X’ and ‘Y’ we will get a array say ‘result’ that is lexicographically maximum, then ‘X’ and ‘Y’ must be lexicographically maximum possible also. For example let ‘X be [ ‘x1’, ‘x2’] and ‘Y’ be [ ‘y1’, ‘y2’ ] and ‘result’ be [ ‘ x1’ , ‘y1’, ‘x2’, ‘y2’], Now we say that the ‘result’ is lexicographically maximum, and let there exists another subsequence in ‘arr1’ of length 2 such that it is lexicographically greater than ‘X’ but then ‘result’ cannot be lexicographically maximum.
So now, the problem can be divided into two parts.
To find lexicographically maximum subsequences from an array, we can use stack. We will maintain a non-increasing stack of size of ‘K’ elements. We iterate over the array and pop the elements in the stack until the current element is greater than the top element in the stack and push the current element.
For example, let the array be [ 6, 8, 5, 4, 2, 6, 6, 10 ] and we need a subsequence of size 3.
To merge two subsequences such that we obtain a lexicographically maximum array, we just use the merge technique as done in merge sort, with a slight modification i.e to add each element into a merged array we not only check the current element from both arrays, we have to check subarrays of both arrays starting from the current element to the length of the arrays, and then we add the element of an array whose subarray is lexicographically greater. For example, if the arrays are [2, 2, 4] and [2, 2, 2, 5]. For the first element of the merged array, we don’t check only the first element of both arrays as done in merge sort, here we have to check subarrays of both arrays starting from index 0 to length of both arrays i.e [2, 2, 4] and [2, 2, 2, 5]. And we can see that the first array is lexicographically bigger. So we add ‘2’ of the first array into ‘merged’.
The Function compares two arrays for lexicographic order, it will return true if the subarray ‘firstArr[ ‘idx1’ : size of ‘firstArr’ ]’ is lexicographically bigger than ‘secArr [ ‘idx2’ : size of ‘secArr’ ]’ .
The function will take two parameters:
bool isGreater ( ‘firstArr’, ‘secArr’, ‘idx1’, ‘idx2’ ):
This function will return the lexicographically maximum possible subsequence of length ‘len’ from ‘arr’.
The function will take two parameters:
array getSeq ( ‘arr’, ‘len’ ):
This function will merge two given arrays such that the merged array is lexicographically maximum.
The function will take two parameters:
array merge ( ‘firstArr’, ‘secArr’, ‘idx1’, ‘idx2’ ):
O(K * (N + M + K ^ 2)), where ‘K’ is the size of the required array, ‘N’ and ‘M’ are the size of the given array.
In the worst-case ‘getSeq’ function will take O(N) time for the first array and O (M) for the second array. ‘merge’ function takes O (K ^ 2) time in the worst case, as an array to merge can have maximum length ‘K’ and its inner loop i.e. ‘isGreater’ function takes O(K) in the worst case. And we are calling these functions ‘K’ times. Hence the overall time complexity will be O(K) * ( O( N + M ) + O( K ^ 2) ) = O (K * (N + M + K ^ 2)).
O(K), where ‘K’ is the size of the required array.
In the worst case, we are using an array of size ‘K’ for the ‘getSeq’ function and a stack that can have max length ‘K’, and also an array to store the resulting array.