fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. // Optimize I/O speed
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(NULL);
  10.  
  11. int n;
  12. if (!(cin >> n)) return 0;
  13.  
  14. vector<long long> a(n), b(n);
  15. for (int i = 0; i < n; i++) cin >> a[i];
  16. for (int i = 0; i < n; i++) cin >> b[i];
  17.  
  18. int i = 0, j = 0;
  19.  
  20. // Merge the 2 arrays using the two pointers technique
  21. while (i < n && j < n) {
  22. if (a[i] <= b[j]) {
  23. cout << a[i] << " ";
  24. i++;
  25. } else {
  26. cout << b[j] << " ";
  27. j++;
  28. }
  29. }
  30.  
  31. // Print remaining elements of array A (if any)
  32. while (i < n) {
  33. cout << a[i] << " ";
  34. i++;
  35. }
  36.  
  37. // Print remaining elements of array B (if any)
  38. while (j < n) {
  39. cout << b[j] << " ";
  40. j++;
  41. }
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty