Maximum XOR using K numbers from 1 to n - GeeksforGeeks

15 Feb, 2022
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 ‘n’ itself. Now for k > 1 we can always have an number with its all bits set till the most significant set bit in ‘n’.
C++
Java
Python
C#
PHP
Javascript
// CPP program to find max xor sum// of 1 to n using atmost k numbers#include <bits/stdc++.h>using namespace std;typedef long long int ll; // To return max xor sum of 1 to n// using at most k numbersll maxXorSum(ll n, ll 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    ll res = 1;    while (res <= n)        res <<= 1;     // Return res - 1 which denotes    // a number with all bits set to 1    return res - 1;} // Driver programint main(){    ll n = 4, k = 3;    cout << maxXorSum(n, k);    return 0;}
// Java program to find max xor sum// of 1 to n using atmost k numberspublic 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));    }}
# 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 numbersdef 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 coden = 4k = 3print( maxXorSum(n, k) ) # This code is contributed by Abhishek Sharma44.
// C# program to find max xor sum// of 1 to n using atmost k numbersusing 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.
<?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 numbersfunction 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?>
<script> // JavaScript 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        let 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                 let n = 4, k = 3;        document.write(maxXorSum(n, k)); </script>
Output : 
7
 
YouTubeGeeksforGeeks500K subscribersMaximum XOR using K numbers from 1 to n | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:39•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=Jjlk_BqLK7Q" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
Mithun Kumar
vikashkumar317
splevel62
shivamaggarwal5570
Bitwise-XOR
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Set, Clear and Toggle a given bit of a number in C
Program to find parity
Hamming code Implementation in C/C++
Implementation of Bit Stuffing and Bit Destuffing
Write an Efficient Method to Check if a Number is Multiple of 3
Check whether K-th bit is set or not
Builtin functions of GCC compiler
Count total bits in a number
Check for Integer Overflow
Swap all odd and even bits