fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void compareMerge(vector<int>& a1,vector<int>& a2){
  4. int a=a1.size();
  5. int b=a2.size();
  6. int l=a-1; //start from the last element of first array
  7. int r=0; //start from the first element of second array
  8. while(l>=0 && r<b){
  9. if(a1[l]>a2[r]){
  10. swap(a1[l],a2[r]);
  11. l--;
  12. r++;
  13. }
  14. else{
  15. break;
  16. }
  17. }
  18. sort(a1.begin(),a1.end());
  19. sort(a2.begin(),a2.end());
  20. }
  21.  
  22. int main() {
  23. // your code goes here
  24. vector<int>a1={1,3,4,5};
  25. vector<int>a2={2,4,6,8};
  26. int n1=a1.size();
  27. int n2=a2.size();
  28. compareMerge(a1,a2);
  29. for(int i=0;i<n1;i++){
  30. cout<<a1[i];
  31. }
  32. for(int j=0;j<n2;j++){
  33. cout<<a2[j];
  34. }
  35. return 0;
  36. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
12344568