# Value in a given range with maximum XOR

Given positive integers N, L, and R, we have to find the maximum value of N ⊕ X, where X ∈ [L, R].**Examples:**

Input :N = 7

L = 2

R = 23Output :23

Explanation : When X = 16, we get 7 ⊕ 16 = 23 which is the maximum value for all X ∈ [2, 23].Input :N = 10

L = 5

R = 12Output :15

Explanation : When X = 5, we get 10 ⊕ 5 = 15 which is the maximum value for all X ∈ [5, 12].Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

DSA Self Paced Courseat a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please referComplete Interview Preparation Course.In case you wish to attend

live classeswith experts, please referDSA Live Classes for Working ProfessionalsandCompetitive Programming Live for Students.

**Brute force approach**: We can solve this problem using brute force approach by looping over all integers over the range [L, R] and taking their XOR with N while keeping a record of the maximum result encountered so far. The complexity of this algorithm will be O(R – L), and it is not feasible when the input variables approach high values such as 10^{9}.**Efficient approach**: Since the XOR of two bits is 1 if and only if they are complementary to each other, we need X to have complementary bits to that of N to have the maximum value. We will iterate from the largest bit (log_{2}(R)^{th} Bit) to the lowest (0^{th} Bit). The following two cases can arise for each bit:

- If the bit is not set, i.e. 0, we will try to set it in X. If setting this bit to 1 results in X exceeding R, then we will not set it.

- If the bit is set, i.e. 1, then we will try to unset it in X. If the current value of X is already greater than or equal to L, then we can safely unset the bit. In the other case, we will check if setting all of the next bits is enough to keep X >= L. If not, then we are required to set the current bit. Observe that setting all the next bits is equivalent to adding (1 <<
*b*) – 1, where*b*is the current bit.

The time complexity of this approach is O(log_{2}(R)).

## C++

`// CPP program to find the x in range [l, r]` `// such that x ^ n is maximum.` `#include <cmath>` `#include <iostream>` `using` `namespace` `std;` `// Function to calculate the maximum value of` `// N ^ X, where X is in the range [L, R]` `int` `maximumXOR(` `int` `n, ` `int` `l, ` `int` `r)` `{` ` ` `int` `x = 0;` ` ` `for` `(` `int` `i = log2(r); i >= 0; --i)` ` ` `{` ` ` `if` `(n & (1 << i)) ` `// Set bit` ` ` `{` ` ` `if` `(x + (1 << i) - 1 < l)` ` ` `x ^= (1 << i);` ` ` `}` ` ` `else` `// Unset bit` ` ` `{` ` ` `if` `((x ^ (1 << i)) <= r)` ` ` `x ^= (1 << i);` ` ` `}` ` ` `}` ` ` `return` `n ^ x;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `n = 7, l = 2, r = 23;` ` ` `cout << ` `"The output is "` `<< maximumXOR(n, l, r);` ` ` `return` `0;` `}` |

## Java

`// Java program to find the x in range [l, r]` `// such that x ^ n is maximum.` `import` `java.util.*;` `import` `java.lang.*;` `import` `java.io.*;` `class` `GFG` `{` `// Function to calculate the maximum value of` `// N ^ X, where X is in the range [L, R]` `static` `int` `maximumXOR(` `int` `n, ` `int` `l, ` `int` `r)` `{` ` ` `int` `x = ` `0` `;` ` ` `for` `(` `int` `i = (` `int` `)(Math.log(r)/Math.log(` `2` `)); i >= ` `0` `; --i)` ` ` `{` ` ` `if` `((n & (` `1` `<< i))>` `0` `) ` `// Set bit` ` ` `{` ` ` `if` `(x + (` `1` `<< i) - ` `1` `< l)` ` ` `x ^= (` `1` `<< i);` ` ` `}` ` ` `else` `// Unset bit` ` ` `{` ` ` `if` `((x ^ (` `1` `<< i)) <= r)` ` ` `x ^= (` `1` `<< i);` ` ` `}` ` ` `}` ` ` `return` `n ^ x;` `}` `// Driver function` `public` `static` `void` `main(String args[])` `{` ` ` `int` `n = ` `7` `, l = ` `2` `, r = ` `23` `;` ` ` `System.out.println( ` `"The output is "` `+ maximumXOR(n, l, r));` `}` `}` `// This code is Contributed by tufan_gupta2000` |

## Python3

`# Python program to find the` `# x in range [l, r] such that` `# x ^ n is maximum.` `import` `math` `# Function to calculate the` `# maximum value of N ^ X,` `# where X is in the range [L, R]` `def` `maximumXOR(n, l, r):` ` ` `x ` `=` `0` ` ` `for` `i ` `in` `range` `(` `int` `(math.log2(r)), ` `-` `1` `, ` `-` `1` `):` ` ` `if` `(n & (` `1` `<< i)): ` `# Set bit` ` ` `if` `(x ` `+` `(` `1` `<< i) ` `-` `1` `< l):` ` ` `x ^` `=` `(` `1` `<< i)` ` ` `else` `: ` `# Unset bit` ` ` `if` `(x ^ (` `1` `<< i)) <` `=` `r:` ` ` `x ^` `=` `(` `1` `<< i)` ` ` `return` `n ^ x` `# Driver code` `n ` `=` `7` `l ` `=` `2` `r ` `=` `23` `print` `(` `"The output is"` `,` ` ` `maximumXOR(n, l, r))` `# This code was contributed` `# by VishalBachchas` |

## C#

`// C# program to find the x in range` `// [l, r] such that x ^ n is maximum.` `using` `System;` `class` `GFG` `{` ` ` `// Function to calculate the` `// maximum value of N ^ X,` `// where X is in the range [L, R]` `public` `static` `int` `maximumXOR(` `int` `n,` ` ` `int` `l, ` `int` `r)` `{` ` ` `int` `x = 0;` ` ` `for` `(` `int` `i = (` `int` `)(Math.Log(r) /` ` ` `Math.Log(2)); i >= 0; --i)` ` ` `{` ` ` `if` `((n & (1 << i)) > 0) ` `// Set bit` ` ` `{` ` ` `if` `(x + (1 << i) - 1 < l)` ` ` `{` ` ` `x ^= (1 << i);` ` ` `}` ` ` `}` ` ` `else` `// Unset bit` ` ` `{` ` ` `if` `((x ^ (1 << i)) <= r)` ` ` `{` ` ` `x ^= (1 << i);` ` ` `}` ` ` `}` ` ` `}` ` ` `return` `n ^ x;` `}` `// Driver Code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` `int` `n = 7, l = 2, r = 23;` ` ` `Console.WriteLine(` `"The output is "` `+` ` ` `maximumXOR(n, l, r));` `}` `}` `// This code is contributed` `// by Shrikant13` |

## PHP

`<?php` `// PHP program to find the x in range` `// [l, r] such that x ^ n is maximum.` `// Function to calculate the maximum` `// value of N ^ X, where X is in the` `// range [L, R]` `function` `maximumXOR(` `$n` `, ` `$l` `, ` `$r` `)` `{` ` ` `$x` `= 0;` ` ` `for` `(` `$i` `= log(` `$r` `, 2); ` `$i` `>= 0; --` `$i` `)` ` ` `{` ` ` `if` `(` `$n` `& (1 << ` `$i` `))` ` ` `{ ` ` ` `// Set bit` ` ` `if` `(` `$x` `+ (1 << ` `$i` `) - 1 < ` `$l` `)` ` ` `$x` `^= (1 << ` `$i` `);` ` ` `}` ` ` `else` ` ` `{` ` ` `// Unset bit` ` ` `if` `((` `$x` `^ (1 << ` `$i` `)) <= ` `$r` `)` ` ` `$x` `^= (1 << ` `$i` `);` ` ` `}` ` ` `}` ` ` `return` `$n` `^ ` `$x` `;` `}` `// Driver Code` `$n` `= 7;` `$l` `= 2;` `$r` `= 23;` `echo` `"The output is "` `,` ` ` `maximumXOR(` `$n` `, ` `$l` `, ` `$r` `);` `// This code is contributed by ajit` `?>` |

## Javascript

`<script>` `// Javascript program to find` `// the x in range [l, r]` `// such that x ^ n is maximum.` `// Function to calculate the maximum value of` `// N ^ X, where X is in the range [L, R]` `function` `maximumXOR(n, l, r)` `{` ` ` `let x = 0;` ` ` `for` `(let i =` ` ` `parseInt(Math.log(r) / Math.log(2)); i >= 0; --i)` ` ` `{` ` ` `if` `(n & (1 << i)) ` `// Set bit` ` ` `{` ` ` `if` `(x + (1 << i) - 1 < l)` ` ` `x ^= (1 << i);` ` ` `}` ` ` `else` `// Unset bit` ` ` `{` ` ` `if` `((x ^ (1 << i)) <= r)` ` ` `x ^= (1 << i);` ` ` `}` ` ` `}` ` ` `return` `n ^ x;` `}` `// Driver Code` ` ` `let n = 7, l = 2, r = 23;` ` ` `document.write(` `"The output is "` `+ maximumXOR(n, l, r));` ` ` `</script>` |

**Output**

The output is 23