C++ || Greedy || Commented || Easy to Understand
\nclass Solution {\npublic:\n    \n    //function to find the maximum lexicographic sequence using monotonic stack implemented\n    //via vector. It will give u
```\nclass Solution {\npublic:\n    \n    //function to find the maximum lexicographic sequence using monotonic stack implemented\n    //via vector. It will give us maximum lexicographic sequence of length k.\n    \n    vector<int> maxLex(vector<int> nums, int k) {\n        int N = nums.size();\n        vector<int> res;\n        \n        for(int i=0; i<N; i++) {\n            //condition to pop\n            //the last condition ensures length k is returned\n            while(!res.empty() and nums[i] > res.back() and k - (int)res.size() <= N - i - 1) {\n                res.pop_back();\n            }\n            if(res.size() < k)\n                res.push_back(nums[i]);\n        }\n        return res;\n    }\n    \n    //function to return true if vector starting at a is lexicographically greater than nums2 starting at b\n    \n    bool greater(vector<int>& nums1, vector<int>& nums2, int a, int b) {\n        while(a < nums1.size() or b < nums2.size()) {\n            if(a >= nums1.size())\n                return false;\n            else if(b >= nums2.size())\n                return true;\n            else if(nums1[a] < nums2[b]) \n                return false;\n            else if(nums1[a] > nums2[b])\n                return true;\n            else \n                ++a,++b;\n        }\n        return true;\n    }\n    \n    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {\n        vector<int> ans;\n        //we are trying to generate all possible max lexicographic sequences from the two vextors\n        //of different lengths and then we merge them and compare for maximum result\n        for(int i=0; i<=k; i++) {\n            int j = k - i;\n            \n            if(i > nums1.size() or j > nums2.size())\n                continue;\n            \n            vector<int> max1 = maxLex(nums1,i);\n            vector<int> max2 = maxLex(nums2,j);\n            \n            //using merge sort technique\n            vector<int> merged;\n            \n            int a = 0;\n            int b = 0;\n            \n            while(a < max1.size() or b < max2.size()) {\n                if(a >= max1.size())\n                    merged.push_back(max2[b++]);\n                else if(b >= max2.size())\n                    merged.push_back(max1[a++]);\n                else if(max1[a] < max2[b]) \n                    merged.push_back(max2[b++]);\n                else if(max1[a] > max2[b])\n                    merged.push_back(max1[a++]);\n                //if we have the case where we have got the same value then we have to consider \n                //which of the two vectors have max lexicographic sequence beginning from a and b pointers\n                //as a max sequence will affect the answer positively and we would be pushing the result\n                //from the vector which has a max sequence\n                else{\n                    if(greater(max1,max2,a,b)) {\n                    //if max1 is greater then we enter\n                        merged.push_back(max1[a++]);\n                    }\n                    else {\n                        merged.push_back(max2[b++]);\n                    }\n                }\n            }\n            if(merged > ans)\n                ans = merged;\n        }\n        return ans;\n    }\n};\n```
['Stack', 'Greedy', 'C']