Given an integer **A**, the task is to find the maximum value possible(**B**) which is less than A, such that xor of these two numbers A and B is equal their sum, that is **A ^ B = A + B**.

**Examples:**

Input:A = 4Output:3Explanation:

There are many such integers such that A ^ B = A + B

Some of this integers are –

4 ^ 3 = 4 + 3 = 7

4 ^ 2 = 4 + 2 = 6

4 ^ 1 = 4 + 1 = 5

4 ^ 0 = 4 + 0 = 4

Maximum of these values is 3

Input:7Output:0

There is no integer except 0 such that A + B = A ^ B

**Approach:** The idea is to use the fact that

and to get the value of

, the value of (A & B) must be equal to 0.

=> A & B = 0 => B = ~A

**For Example:**

A = 4 (1 0 0)B = ~ A = (0 1 1) = 3

Below is the implementation of the above approach:

## C++

`// C++ implementation to find ` `// maximum value of B such that ` `// A ^ B = A + B ` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the maximum ` `// value of B such that A^B = A+B ` `void` `maxValue(` `int` `a) ` `{ ` ` ` ` ` `// Binary Representation of A ` ` ` `string c = bitset<3>(a).to_string();` ` ` `string b = ` `""` `; ` ` ` ` ` `// Loop to find the negation ` ` ` `// of the integer A ` ` ` `for` `(` `int` `i = 0; i < c.length(); i++) ` ` ` `{ ` ` ` `if` `((c[i] - ` `'0'` `) == 1) ` ` ` `b += ` `'0'` `; ` ` ` `else` ` ` `b += ` `'1'` `; ` ` ` `} ` ` ` ` ` `// Output ` ` ` `cout << bitset<3>(b).to_ulong(); ` `} ` `// Driver code` `int` `main()` `{` ` ` `int` `a = 4; ` ` ` ` ` `// Function Call ` ` ` `maxValue(a);` ` ` ` ` `return` `0;` `}` `// This code is contributed by divyeshrabadiya07` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find` `// maximum value of B such that` `// A ^ B = A + B` ` ` `// Function to find the maximum` `// value of B such that A^B = A+B` `class` `GFG` `{` `static` `void` `maxValue(` `int` `a)` `{` ` ` ` ` `// Binary Representation of A` ` ` `String c = Integer.toBinaryString(a);` ` ` ` ` `String b = ` `""` `;` ` ` ` ` `// Loop to find the negation` ` ` `// of the integer A` ` ` `for` `(` `int` `i = ` `0` `; i < c.length(); i++)` ` ` `{` ` ` `if` `((c.charAt(i)-` `'0'` `)==` `1` `)` ` ` `b +=` `'0'` `;` ` ` `else` ` ` `b+=` `'1'` `;` ` ` `}` ` ` ` ` `// output` ` ` `System.out.print(Integer.parseInt(b, ` `2` `));` ` ` `}` ` ` `// Driver Code` `public` `static` `void` `main(String []args)` `{` ` ` `int` `a = ` `4` `;` ` ` ` ` `// Function Call` ` ` `maxValue(a);` `}` `}` `// This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find` `# maximum value of B such that` `# A ^ B = A + B` `# Function to find the maximum` `# value of B such that A^B = A+B` `def` `maxValue(a):` ` ` ` ` `# Binary Representation of A` ` ` `a ` `=` `bin` `(a)[` `2` `:]` ` ` ` ` `b ` `=` `''` ` ` ` ` `# Loop to find the negation` ` ` `# of the integer A` ` ` `for` `i ` `in` `list` `(a):` ` ` `b ` `+` `=` `str` `(` `int` `(` `not` `int` `(i)))` ` ` ` ` `# output` ` ` `print` `(` `int` `(b, ` `2` `))` ` ` `return` `int` `(b, ` `2` `)` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `4` ` ` ` ` `# Function Call` ` ` `maxValue(a)` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find` `// maximum value of B such that` `// A ^ B = A + B` ` ` `// Function to find the maximum` `// value of B such that A^B = A+B` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG` `{` ` ` `static` `void` `maxValue(` `int` `a)` `{` ` ` ` ` `// Binary Representation of A` ` ` `String c = Convert.ToString(a, 2);` ` ` ` ` `String b = ` `""` `;` ` ` ` ` `// Loop to find the negation` ` ` `// of the integer A` ` ` `for` `(` `int` `i = 0; i < c.Length; i++)` ` ` `{` ` ` `if` `((c[i] - ` `'0'` `) == 1)` ` ` `b += ` `'0'` `;` ` ` `else` ` ` `b += ` `'1'` `;` ` ` `}` ` ` ` ` `// output` ` ` `Console.Write(Convert.ToInt32(b, 2));` ` ` `}` ` ` `// Driver Code` `public` `static` `void` `Main(String []args)` `{` ` ` `int` `a = 4;` ` ` ` ` `// Function Call` ` ` `maxValue(a);` `}` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output:**

3

**Performance Analysis:**

**Time Complexity:**In the above-given approach, there is conversion from decimal to binary which takes O(logN) time in worst case. Therefore, the time complexity for this approach will be**O(logN)**.**Auxiliary Space Complexity:**In the above-given approach, there is no extra space used. Therefore, the auxiliary space complexity for the above approach will be**O(1)**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum profit such that total stolen value is less than K to get bonus
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Find three integers less than or equal to N such that their LCM is maximum
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Largest number less than N with digit sum greater than the digit sum of N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Count of elements having Euler's Totient value one less than itself
- Find unique pairs such that each element is less than or equal to N
- Print a number strictly less than a given number such that all its digits are distinct.
- Find largest factor of N such that N/F is less than K
- Largest number M less than N such that XOR of M and N is even
- Find maximum product of digits among numbers less than or equal to N
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Number less than equals to N with maximum product of prime factors
- Maximum sum subarray of size K with sum less than X
- Smallest subarray of size greater than K with sum greater than a given value
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Nearest prime less than given number n

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.