Create Maximum Number in C++

Suppose we have two arrays of length m and n with digits 0-9 representing two numbers. We have to create the maximum number of length k that is less than m + n from digits of the two. We have to keep in mind that the relative order of the digits from the same array must be preserved. We have to find the array of the k digits. So if the inputs are like [3,4,7,5] and [9,1,3,5,8,4], and k = 5, then the answer will be [9,8,7,5,4].
To solve this, we will follow these steps −
Define a function mergeThem(), this will take an array nums1, an array nums2,
Define an array ret
i := 0, j := 0, n := size of nums1, m := size of nums2
while (i < n or j < m), do −if call the function greater(nums1, nums2, i, j) is true, then −insert nums1[i] at the end of ret(increase i by 1)Otherwiseinsert nums2[j] at the end of ret(increase j by 1)
if call the function greater(nums1, nums2, i, j) is true, then −insert nums1[i] at the end of ret(increase i by 1)
insert nums1[i] at the end of ret
(increase i by 1)
Otherwiseinsert nums2[j] at the end of ret(increase j by 1)
insert nums2[j] at the end of ret
(increase j by 1)
return ret
Define a function modify(), this will take an array v, k,
Define one stack st
Define an array ret
for initialize i := 0, when i < size of v, update (increase i by 1), do −x := v[i]while (st is not empty and top element of st < x and size of st + size of v – i – 1 >= k), do −delete element from stif size of st < k, then −insert x into st
x := v[i]
while (st is not empty and top element of st < x and size of st + size of v – i – 1 >= k), do −delete element from st
delete element from st
if size of st < k, then −insert x into st
insert x into st
while (st is not empty), do −insert top element of st at the end of retdelete element from st
insert top element of st at the end of ret
delete element from st
reverse the array ret
return ret
Define a function greater(), this will take an array a, an array b, i, j,
while (i < size of a and j < size of b and a[i] is same as b[j]), do −increase i by 1 and increase j by 1
increase i by 1 and increase j by 1
return true when j == size of b or i < size of a and a[i] > b[j]
From the main method do the following:
Define an array ret
n := size of nums1
m := size of nums2
for initialize i := 0, when i <= k, update (increase i by 1), do −if i <= n and (k - i) <= m, then −Define an array candidate = mergeThem(modify(nums1, i), modify(nums2, k - i))if greater(candidate, ret, 0, 0) is true, then −ret := candidate
if i <= n and (k - i) <= m, then −Define an array candidate = mergeThem(modify(nums1, i), modify(nums2, k - i))if greater(candidate, ret, 0, 0) is true, then −ret := candidate
Define an array candidate = mergeThem(modify(nums1, i), modify(nums2, k - i))
if greater(candidate, ret, 0, 0) is true, then −
ret := candidate
return ret
Let us see the following implementation to get better understanding −
 Live Demo
#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
public:
   vector<int> mergeThem(vector<int> nums1, vector<int> nums2)
   {
      vector<int> ret;
      int i = 0;
      int j = 0;
      int n = nums1.size();
      int m = nums2.size();
      while (i < n || j < m) {
         if (greater(nums1, nums2, i, j)) {
            ret.push_back(nums1[i]);
            i++;
         }
         else {
            ret.push_back(nums2[j]);
            j++;
         }
      }
      return ret;
   }
   vector<int> modify(vector<int>& v, int k)
   {
      stack<int> st;
      vector<int> ret;
      for (int i = 0; i < v.size(); i++) {
         int x = v[i];
         while (!st.empty() && st.top() < x && st.size() + (v.size() - i) - 1 >= k) {
            st.pop();
         }
         if (st.size() < k)
            st.push(x);
         }
         while (!st.empty()) {
            ret.push_back(st.top());
            st.pop();
         }
         reverse(ret.begin(), ret.end());
         return ret;
      }
      bool greater(vector<int>& a, vector<int>& b, int i, int j)
      {
         while (i < a.size() && j < b.size() && a[i] == b[j])
         i++, j++;
         return j == b.size() || (i < a.size() && a[i] > b[j]);
      }
      vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k)
      {
         vector<int> ret;
         int n = nums1.size();
         int m = nums2.size();
         for (int i = 0; i <= k; i++) {
            if (i <= n && (k - i) <= m) {
               vector<int> candidate = mergeThem(modify(nums1, i), modify(nums2, k - i));
               if (greater(candidate, ret, 0, 0)) {
                  ret = candidate;
               }
            }
         }
         return ret;
     }
};
main() {
   Solution ob;
   vector<int> v = { 3, 4, 7, 5 }, v1 = { 9, 1, 3, 5, 8, 4 };
   print_vector(ob.maxNumber(v, v1, 5));
}
{ 3, 4, 7, 5 }
{ 9, 1, 3, 5, 8, 4 }
5
[9, 8, 7, 5, 4, ]