#C2033. Find Two Unique Numbers
Find Two Unique Numbers
Find Two Unique Numbers
You are given an array of integers in which every integer appears exactly twice, except for two numbers that appear only once. Your task is to find these two unique numbers and print them in ascending order.
Consider the following properties:
- All numbers appear exactly twice except the two unique numbers.
- Let \( x_1 \) and \( x_2 \) be the unique numbers. You need to return the sorted order: \( \min(x_1, x_2) \) followed by \( \max(x_1, x_2) \).
Hint: You may use the properties of the bitwise XOR operator \( (\oplus) \). In fact, if you XOR all the numbers, the duplicate numbers will cancel out, leaving \( x_1 \oplus x_2 \). Then, by extracting the rightmost set bit from \( x_1 \oplus x_2 \), you can partition the array into two groups to identify the unique numbers.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains an integer \( n \) — the number of elements in the array.
- The second line contains \( n \) space-separated integers representing the array elements.
Note that \( n \) is always even and the array will always contain exactly two unique numbers with all other numbers appearing exactly twice.
outputFormat
The output should be written to stdout and consist of two integers separated by a space. These integers are the two unique numbers in ascending order.
## sample6
1 2 3 2 1 4
3 4