Maximum XOR using K numbers from 1 to n
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.
Examples :
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.
C++
// CPP program to find max xor sum // of 1 to n using atmost k numbers #include <bits/stdc++.h> using namespace std; // To return max xor sum of 1 to n // using at most k numbers int maxXorSum(int n, int k) { // If k is 1 then maximum // possible sum is n if (k == 1) return n; // Finding number greater than // or equal to n with most significant // bit same as n. For example, if n is // 4, result is 7. If n is 5 or 6, result // is 7 int res = 1; while (res <= n) res <<= 1; // Return res - 1 which denotes // a number with all bits set to 1 return res - 1; } // Driver program int main() { int n = 4, k = 3; cout << maxXorSum(n, k); return 0; } |
chevron_right
filter_none
Java
// Java program to find max xor sum // of 1 to n using atmost k numbers public class Main { // To return max xor sum of 1 to n // using at most k numbers static int maxXorSum(int n, int k) { // If k is 1 then maximum // possible sum is n if (k == 1) return n; // Finding number greater than // or equal to n with most significant // bit same as n. For example, if n is // 4, result is 7. If n is 5 or 6, result // is 7 int res = 1; while (res <= n) res <<= 1; // Return res - 1 which denotes // a number with all bits set to 1 return res - 1; } // Driver program to test maxXorSum() public static void main(String[] args) { int n = 4, k = 3; System.out.print(maxXorSum(n, k)); } } |
chevron_right
filter_none
Python
# Python3 code to find max xor sum # of 1 to n using atmost k numbers # To return max xor sum of 1 to n # using at most k numbers def maxXorSum( n , k ): # If k is 1 then maximum # possible sum is n if k == 1: return n # Finding number greater than # or equal to n with most significant # bit same as n. For example, if n is # 4, result is 7. If n is 5 or 6, result # is 7 res = 1 while res <= n: res <<= 1 # Return res - 1 which denotes # a number with all bits set to 1 return res - 1 # Driver code n = 4k = 3print( maxXorSum(n, k) ) # This code is contributed by Abhishek Sharma44. |
chevron_right
filter_none
C#
// C# program to find max xor sum // of 1 to n using atmost k numbers using System; public class main { // To return max xor sum of 1 to n // using at most k numbers static int maxXorSum(int n, int k) { // If k is 1 then maximum // possible sum is n if (k == 1) return n; // Finding number greater than // or equal to n with most significant // bit same as n. For example, if n is // 4, result is 7. If n is 5 or 6, result // is 7 int res = 1; while (res <= n) res <<= 1; // Return res - 1 which denotes // a number with all bits set to 1 return res - 1; } // Driver program public static void Main() { int n = 4, k = 3; Console.WriteLine(maxXorSum(n, k)); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP program to find max xor sum // of 1 to n using atmost k numbers // To return max xor sum of 1 to n // using at most k numbers function maxXorSum($n, $k) { // If k is 1 then maximum // possible sum is n if ($k == 1) return $n; // Finding number greater than // or equal to n with most // significant bit same as n. // For example, if n is 4, result // is 7. If n is 5 or 6, result is 7 $res = 1; while ($res <= $n) $res <<= 1; // Return res - 1 which denotes // a number with all bits set to 1 return $res - 1; } // Driver code $n = 4; $k = 3; echo maxXorSum($n, $k); // This code is contributed by Mithun Kumar ?> |
chevron_right
filter_none
Output :
7
Recommended Posts:
- Maximum XOR of Two Numbers in an Array
- Maximum sum by adding numbers with same number of set bits
- Maximum XOR value in matrix
- Maximum XOR-value of at-most k-elements from 1 to n
- Value in a given range with maximum XOR
- Maximum AND value of a pair in an array
- Find a value whose XOR with given number is maximum
- Find a sub matrix with maximum XOR
- Maximum value of XOR among all triplets of an array
- Maximum 0's between two immediate 1's in binary representation
- Find the maximum subset XOR of a given set
- Find the node whose xor with x gives maximum value
- Maximum XOR value of a pair from a range
- Maximum steps to transform 0 to X with bitwise AND
- Maximum Bitwise AND pair from given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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



