CodeChef: June Challenge | Optimal Xor Set OPTSET | Python, Java, C++ Solution


View problem on codechef

Find KK distinct numbers in the range [1,N][1,N] such that the bitwise XOR of all the numbers is maximized. Print any set of these numbers that maximize the XOR.


  • The first line contains an integer TT, the number of test cases. Then the test cases follow.
  • Each test case contains a single line of input, two integers NN, KK.


For each test case, output KK distinct integers in any order as described in the problem.


  • 1≤T≤1041≤T≤104
  • 1≤K≤N≤1061≤K≤N≤106
  • The sum of NN over all queries is at most 4⋅1064⋅106.
  • The sum of KK over all queries is at most 2⋅1062⋅106.


Subtask #1 (5 points):

  • 1≤T≤501≤T≤50
  • 1≤K≤N≤201≤K≤N≤20

Subtask #2 (20 points):

  • 1≤T≤501≤T≤50
  • 1≤K≤N≤2001≤K≤N≤200
  • The sum of NN over all queries is at most 800800.
  • The sum of KK over all queries is at most 400400.

Subtask #3 (75 points): original constraints

Sample Input

10 1
9 2
8 7

Sample Output

7 8
1 2 3 4 5 6 8


Test Case 1: The only possible set is {10}{10} which gives the value 1010.

Test Case 2: The other possible set is {9,6}{9,6} which gives the value 9⊕6=15=7⊕89⊕6=15=7⊕8.

Test Case 3: The only possible set is {1,2,3,4,5,6,8}{1,2,3,4,5,6,8} which gives the value 1⊕2⊕3⊕4⊕5⊕6⊕8=151⊕2⊕3⊕4⊕5⊕6⊕8=15.


Due to copyright issues we won't be able to update the solution here immediately. However you can download the code file from our telegram channel. So join our telegram channel for further updates. Keep tracking, the solution will be updated on this website soon.

Read more posts here

Read more post Here

Leave a Comment

Your email address will not be published. Required fields are marked *