Natural XOR elements
Practice
3.3 (69 votes)
Bit manipulation
Basic programming
Basics of bit manipulation
Xor
Basics of bit manipulation
Problem
92% Success 6222 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an integer $$N$$. To solve the problem, you must find the minimum number of elements that must be removed from the set $$S = \lbrace 1, 2, ..., N \rbrace $$ such that the bitwise XOR of the remaining elements is $$0$$.

Input format

  • The first line contains an integer $$t (1 \leq t \leq 10^5)$$ representing the number of test cases.
  • The first and only line of each test case contains an integer $$N (1 \leq N \leq 10^9)$$ representing the number presented to you.

Output format

For each test case, print a single line.

The first integer $$k$$ represents the minimum number of elements and you must remove from $$S$$. Then, $$k$$ space-separated integers $$a_1, a_2, ..., a_k$$ follows representing the elements that you have to remove from $$S$$.

If there are multiple possible solutions, output the lexicographically largest one. A solution $$a_1, a_2, ..., a_k$$ is lexicographically larger than solution $$b_1, b_2, ..., b_k$$, if $$a_i > b_i$$ where $$i$$ is the smallest index where $$a_i \neq b_i$$. 

It can be proved that in an optimal solution $$k \leq 31$$.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
20 votes
Tags:
Bit ManipulationBasic ProgrammingBasics of Bit Manipulation
Points:30
914 votes
Tags:
EasyMathNumber Theory
Points:30
104 votes
Tags:
ApprovedBit ManipulationEasyReady
Editorial

Login to unlock the editorial