Merge two sorted arrays EASY Interclasare (BAC) 10 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

Two arrays of integers are given, each already sorted in non-decreasing order. Print the array obtained by merging the two arrays so that the result is also sorted in non-decreasing order. The required algorithm is the two-pointer merge: compare the current elements of the two arrays and place the smaller one into the result, advancing the corresponding index.

Input format

Input input.txt

The program reads from standard input, in this order: - a natural number `n`, followed by `n` integers representing the first array, sorted in non-decreasing order; - a natural number `m`, followed by `m` integers representing the second array, sorted in non-decreasing order. - `1 <= n, m <= 1000` - values are integers in the range `[-1000000, 1000000]` - the input arrays are already sorted in non-decreasing order (not necessarily strictly) - when both arrays contain equal values, the one from array `a` comes first

Output format

Output output.txt

The program prints to standard output, on a single line, the `n + m` values resulting from the merge, separated by a single space, followed by a newline.

Example

input
5
1 3 5 7 9
4
2 4 6 8
output
1 2 3 4 5 6 7 8 9

Ready to solve this challenge?

Create a free account to write code, submit solutions, and track your progress.

⌨️ Keyboard Shortcuts

Code Editor
Run Code
Ctrl Enter
Submit Code
Ctrl Shift Enter
Format Code
Shift Alt F
Toggle Comment
Ctrl /
Undo
Ctrl Z
Redo
Ctrl Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc

🔍 Interactive Debugger

0 / 0

Analyzing your code...

📦 Variables

No variables yet

📚 Call Stack
main() line 1
📤 Output
We use cookies

Essential cookies are always active. You can choose to enable preference and analytics cookies. Learn more