#K7566. Merge Two Sorted Arrays

    ID: 34469 Type: Default 1000ms 256MiB

Merge Two Sorted Arrays

Merge Two Sorted Arrays

Given two sorted arrays A and B, your task is to merge them into a single sorted array without using any built-in sorting functions. The merging process uses a two-pointer technique and works in \(O(n+m)\) time, where \(n\) and \(m\) are the sizes of the two arrays respectively.

Example: If A = [1, 3, 5] and B = [2, 4, 6], then the merged array will be [1, 2, 3, 4, 5, 6].

inputFormat

The input consists of four lines:

  1. An integer (n) denoting the size of the first sorted array.
  2. (n) space-separated integers in ascending order representing the first array.
  3. An integer (m) denoting the size of the second sorted array.
  4. (m) space-separated integers in ascending order representing the second array. Note: If (n = 0) or (m = 0), the corresponding line will be empty.

outputFormat

Output a single line containing the merged sorted array. The numbers should be printed in ascending order and separated by a single space.## sample

3
1 3 5
3
2 4 6
1 2 3 4 5 6

</p>