
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’.
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.
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’.
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.
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
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
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:
This function will return the lexicographically maximum possible subsequence of length ‘len’ from ‘arr’.
The function will take two parameters:
This function will merge two given arrays such that the merged array is lexicographically maximum.
The function will take two parameters:
In the previous approach, we are using the ‘getSeq’ function to get a lexicographically maximum subsequence of a given size. Another way to find a max lexicographically subsequence of size say ‘i’ can be done using the lexicographically maximum subsequence of size ‘i +1’. Let say the array is of size ‘N’ then lexicographically maximum subsequence of size ‘N’ will be the array itself, now to find a lexicographically maximum subsequence of size ‘N - 1’ we need to delete an element from the first subsequence, and “we can observe that we will delete the first element that is smaller than its next element in the previous subsequence”.
For example let the previous subsequence of length 4 be [ 3, 4, 2, 5], we can get the next lexicographically maximum subsequence with length 3 by deleting 2, as this is the first element that is less than its next element (5). And if the previous subsequence is non -increasing, then we can simply delete the last element to get the next lexicographically maximum subsequence.
For example let the array be [6, 8, 2, 6, 5, 3].
Repeat above for all sizes of subsequence.
So we use two 2-D arrays ‘dp1’ and ‘dp2’ where ‘dp1[i]’ is the lexicographically maximum subsequence of size ‘i’ from ‘arr1’, And dp2[i]’ is the lexicographically maximum subsequence of size ‘i’ from ‘arr2’. We can calculate ‘dp[i]’ from ‘dp[i + 1]’ as above, and ‘dp1[n]’ = ‘arr1’ and ‘dp2[n]’ = ‘arr2’.
The function will take two parameters: