# Maximum XOR-value of at-most k-elements from 1 to n

You are given two positive integer n and k. You have to calculate the maximum possible XOR value of at most k-elements from 1 to n.

**Note:**k > 1

**Examples : **

Input : n = 7, k = 3 Output : 7 Explanation : You can select 1, 2, 4 for maximum XOR-value Input : n = 7, k = 2 Output : 7 Explanation : You can select 3 and 4 for maximum value.

For any value of k we can select atleast two numbers from 1 to n and for the required result we have to take a closer look on the bit-representation of n. So lets understand it through an example. Suppose n = 6 and k = 2:

Bit representation of 6 = 110

Bit representation of 5 = 101

Bit representation of 4 = 100

Bit representation of 3 = 011

Bit representation of 2 = 010

Bit representation of 1 = 001

Now, you can see that after selecting as much numbers you want and selecting any of them you can not obtain XOR value greater than 111 i.e 7. So, for a given n and k >1 the maximum possible XOR value is **2 ^{log2(n)+1}-1** (that is the value when all bits of n are turned to 1).

## C++

`// Program to obtain maximum XOR value sub-array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate maximum XOR value ` `int` `maxXOR(` `int` `n, ` `int` `k) { ` ` ` `int` `c = log2(n) + 1; ` ` ` ` ` `// Return (2^c - 1) ` ` ` `return` `((1 << c) - 1); ` `} ` ` ` `// driver program ` `int` `main() { ` ` ` `int` `n = 12; ` ` ` `int` `k = 3; ` ` ` `cout << maxXOR(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Program to obtain maximum ` `// XOR value sub-array ` `import` `java.lang.*; ` ` ` `class` `GFG ` `{ ` `// function to calculate ` `// maximum XOR value ` `static` `int` `maxXOR(` `int` `n, ` `int` `k) ` `{ ` `int` `c = (` `int` `) (Math.log(n) / ` ` ` `Math.log(` `2` `)) + ` `1` `; ` ` ` `// Return (2^c - 1) ` `return` `((` `1` `<< c) - ` `1` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `int` `n = ` `12` `; ` `int` `k = ` `3` `; ` `System.out.println(maxXOR(n, k)); ` `} ` `} ` ` ` `// This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to obtain maximum ` `# XOR value sub-array ` `import` `math ` ` ` `# Function to calculate maximum XOR value ` `def` `maxXOR(n, k): ` ` ` `c ` `=` `int` `(math.log(n, ` `2` `)) ` `+` `1` ` ` ` ` `# Return (2^c - 1) ` ` ` `return` `((` `1` `<< c) ` `-` `1` `) ` ` ` `# Driver Code ` `n ` `=` `12` `; k ` `=` `3` `print` `(maxXOR(n, k)) ` ` ` `# This code is contributed by shreyanshi_arun. ` |

*chevron_right*

*filter_none*

## C#

`// Program to obtain maximum ` `// XOR value sub-array ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// function to calculate ` `// maximum XOR value ` `static` `int` `maxXOR(` `int` `n, ` `int` `k) ` `{ ` `int` `c = (` `int` `) (Math.Log(n) / ` ` ` `Math.Log(2)) + 1; ` ` ` `// Return (2^c - 1) ` `return` `((1 << c) - 1); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `int` `n = 12; ` `int` `k = 3; ` `Console.Write(maxXOR(n, k)) ; ` `} ` `} ` ` ` `// This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Program to obtain maximum ` `// XOR value sub-array ` ` ` `// function to calculate ` `// maximum XOR value ` `function` `maxXOR(` `$n` `, ` `$k` `) ` `{ ` ` ` `$c` `= log(` `$n` `, 2) + 1; ` ` ` ` ` `// Return (2^c - 1) ` ` ` `return` `((1 << ` `$c` `) - 1); ` `} ` ` ` `// Driver Code ` `$n` `= 12; ` `$k` `= 3; ` `echo` `maxXOR(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

15

## Recommended Posts:

- Maximum XOR using K numbers from 1 to n
- Maximum XOR value in matrix
- Value in a given range with maximum XOR
- Find a value whose XOR with given number is maximum
- Maximum value of XOR among all triplets of an array
- Maximum AND value of a pair in an array
- Find a sub matrix with maximum XOR
- Maximum 0's between two immediate 1's in binary representation
- Maximum XOR value of a pair from a range
- Find the node whose xor with x gives maximum value
- Find the maximum subset XOR of a given set
- Maximum set bit sum in array without considering adjacent elements
- Number whose sum of XOR with given array range is maximum
- Maximum steps to transform 0 to X with bitwise AND
- Find triplets in an array whose AND is maximum

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.