#K73832. Reorder Array for Maximum Bitwise Expression
Reorder Array for Maximum Bitwise Expression
Reorder Array for Maximum Bitwise Expression
You are given an array of non-negative integers. Your task is to reorder the array so that the result of consecutively applying the function
\(g(x, y) = (x \& y) \;|\; (x \;\text{XOR}\; y)\)
to adjacent elements is maximized. It turns out that for any two numbers, \(g(x, y)\) is equivalent to \(x \;|\; y\), and so a descending sort of the array will yield the maximum result when considering the bitwise OR across pairs.
Examples:
- Input:
n = 1, arr = [5]
; Output:5
- Input:
n = 2, arr = [2, 10]
; Output:10 2
(or2 10
is also acceptable since both yield the same maximum value) - Input:
n = 4, arr = [4, 1, 7, 3]
; Output:7 4 3 1
Implement a program that reads the input from stdin and prints the correctly reordered sequence to stdout.
inputFormat
The first line contains an integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated non-negative integers.
outputFormat
Output the reordered array as \(n\) space-separated integers in a single line. The ordering should maximize the bitwise expression defined in the problem.
## sample1
5
5