Symmetric difference of two sorted arrays HARD Interclasare (BAC) 50 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

Two sequences of integers `a` and `b` are given, both sorted in ascending order. Print the values that appear in exactly one of the two sequences (in `a` but not in `b`, or in `b` but not in `a`), each value appearing once, in ascending order. This result is called the symmetric difference `A xor B`. The required solution is a single pass with two indices, in merge style: when the two current values differ, the smaller one enters the result; when they are equal, both values cancel each other and you skip all occurrences of them.

Input format

Input input.txt

The program reads from standard input: - a natural number `n`, followed by `n` integers (sequence `a`); - a natural number `m`, followed by `m` integers (sequence `b`). - `1 <= n, m <= 1000` - `-1000000 <= values <= 1000000` - the sequences are sorted in ascending order and may contain duplicates

Output format

Output output.txt

The program prints on the first line the number `k` of distinct values in the symmetric difference. If `k > 0`, the second line contains the `k` values in ascending order, separated by single spaces. If `k = 0`, no further line is printed.

Example

input
5

1 2 3 4 5

4

2 4 6 8
output
5

1 3 5 6 8

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