Given an positive integer n and k. Find maximum xor of 1 to n using at most k numbers. Xor sum of 1 to n is defined as 1 ^ 2 ^ 3 ^ … ^ n.
Input : n = 4, k = 3 Output : 7 Explanation Maximum possible xor sum is 1 ^ 2 ^ 4 = 7. Input : n = 11, k = 1 Output : 11 Explanation Maximum Possible xor sum is 11.
If we have k = 1 then the maximum possible xor sum is 1. Now for k > 1 we can always have an number with its all bits set to 1. So result will be maximum number greater than n with its all bits set to 1.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum XOR of Two Numbers in an Array
- Maximum sum by adding numbers with same number of set bits
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Maximum value of B less than A such that A ^ B = A + B
- Maximum XOR-value of at-most k-elements from 1 to n
- Value in a given range with maximum XOR
- Maximum XOR value in matrix
- Remove one element to get maximum XOR
- Maximum OR value of a pair in an Array | Set 2
- Maximum AND value of a pair in an array
- Maximum value of XOR among all triplets of an array
- Maximum 0's between two immediate 1's in binary representation
- Find the node whose xor with x gives maximum value
- Maximum XOR value of a pair from a range
- Find a value whose XOR with given number is maximum
- Find the maximum subset XOR of a given set
- Find a sub matrix with maximum XOR
- Find triplets in an array whose AND is maximum
- Maximum frequency of a remainder modulo 2i
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar