Merge three sorted arrays MEDIUM Interclasare (BAC) 25 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

Three sequences of integers are given, each already sorted in ascending order. Print the sequence obtained by merging all three so that the result is also in ascending order. The required algorithm is a generalisation of the two-index merge: use three indices `i`, `j`, `k`, one for each sequence, and at each step print the minimum of `a[i]`, `b[j]` and `c[k]`, advancing the index that provided the minimum. When a sequence is exhausted, treat its current value as `+infinity`.

Input format

Input input.txt

The program reads from standard input, in this order: - a natural number `n1`, followed by `n1` integers (the first sequence); - a natural number `n2`, followed by `n2` integers (the second sequence); - a natural number `n3`, followed by `n3` integers (the third sequence). - `1 <= n1, n2, n3 <= 1000` - `-1000000 <= values <= 1000000` - the sequences are sorted in ascending order and may contain duplicates

Output format

Output output.txt

The program prints to standard output, on a single line, the `n1 + n2 + n3` values in ascending order, separated by single spaces, followed by a newline. When two minima are equal, the value from the first sequence is preferred, then the value from the second sequence.

Example

input
3

1 4 7

3

2 5 8

3

3 6 9
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