Efficient solution using monotonic stack
Intuition\nWe need to consider some numbers from the first array and some from the second array, while maintaining relative ordering, and merge them together to
# Intuition\nWe need to consider some numbers from the first array and some from the second array, while maintaining relative ordering, and merge them together to get the result.\n\nTo do this, we need to calculate for all the possible combinations of the number of numbers that come from each array. Then we find the largest among them.\n\n# Approach\n1. Find the lexicographically largest sequence from the first array with x as the value of k, then find the largest sequence from the second array with y as the value of k using the constraint: `for any x & y where 0 < x \u2264 k and y = k - x;` \nExample: For k = 5.\n\n    | x | y |\n    | --- | --- |\n    | 0 | 5 |\n    | 1 | 4 |\n    | 2 | 3 |\n    | 3 | 2 |\n    | 4 | 1 |\n    | 5 | 0 |\n2. We will find the largest sequence for both the arrays individually and merge them together to get the final result by taking the largest element from each subsequence and incrementing the position.\n3. If we find that the number from both the sub-arrays are the same, we will take the number from the subarray that is lexicographically larger than the other.\n4. We can say that an array \'a\' is lexicographically larger than the array \'b\', if at the position they differ a[i] > b[i] or it could also be that they don\'t differ at any position but the length of a is larger than b.  \n5. Once we have merged both the arrays, we compare it with the other possible combinations that will result from the iteration and we take the largest among them. \n\nIt\'s recommnded that you first solve this related problem which will teach you how to find the smallest subsequence from a single array for a given k: https://leetcode.com/problems/find-the-most-competitive-subsequence\n\n\n\n# Complexity\n- Time complexity:\nO(k * (m + n))\n\n# Code\n```\nfunction maxSingle(nums: number[], k: number): number[] {\n    const stack: number[] = [];\n    for (let i = 0; i < nums.length; ++i) {\n        const num = nums[i];\n        while (\n            stack.length &&\n            stack[stack.length - 1] < num &&\n            nums.length - i > k - stack.length\n        ) stack.pop();\n\n        stack.push(num);\n    }\n\n    return stack.slice(0, k);\n}\n\nfunction lexGreater(nums1: number[], nums2: number[], i: number, j: number): boolean {\n    while (i < nums1.length || j < nums2.length) {\n        if (i >= nums1.length) return false;\n        else if (j >= nums2.length) return true;\n        else if (nums1[i] > nums2[j]) return true;\n        else if (nums2[j] > nums1[i]) return false;\n        else i++, j++;\n    }\n\n    return true;\n}\n\nfunction merge(nums1: number[], nums2: number[]): number[] {\n    let i = 0;\n    let j = 0;\n    const merged: number[] = [];\n    while (i < nums1.length || j < nums2.length) {\n        if (i >= nums1.length) merged.push(nums2[j++]);\n        else if (j >= nums2.length) merged.push(nums1[i++]);\n        else if (nums1[i] < nums2[j]) merged.push(nums2[j++]);\n        else if (nums1[i] > nums2[j]) merged.push(nums1[i++]);\n        else {\n            if (lexGreater(nums1, nums2, i, j)) merged.push(nums1[i++]);\n            else merged.push(nums2[j++]);\n        }\n    }\n\n    return merged;\n}\n\nfunction maxNumber(nums1: number[], nums2: number[], k: number): number[] {\n    let res: number[] = [];\n    for (let i = 0; i <= k; ++i) {\n        let j = k - i;\n        if (i > nums1.length || j > nums2.length) continue;\n        const max1 = maxSingle(nums1, i);\n        const max2 = maxSingle(nums2, j);\n\n        const merged = merge(max1, max2);\n        if (merged > res) {\n            res = merged;\n        }\n    }\n\n    return res; \n};\n```
['Stack', 'Monotonic Stack', 'TypeScript']