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.
Input: A = 4
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
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
A = 4 (1 0 0) B = ~ A = (0 1 1) = 3
Below is the implementation of the above approach:
- 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).
- Auxillary Space Complexity: In the above-given approach, there is no extra space used. Therefore, the auxillary space complexity for the above approach will be O(1)
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum length Subsequence with alternating sign and maximum Sum
- Maximum element in an array such that its previous and next element product is maximum
- Maximum value of |arr[i] - arr[j]| + |i - j|
- Maximum XOR value in matrix
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find maximum value of x such that n! % (k^x) = 0
- Maximum XOR using K numbers from 1 to n
- Maximum value of |arr - arr| + |arr - arr| + ... +|arr[n - 2] - arr[n - 1]| when elements are from 1 to n
- Maximum XOR-value of at-most k-elements from 1 to n
- Value in a given range with maximum XOR
- Maximum array sum that can be obtained after exactly k changes
- Maximum Primes whose sum is equal to given N
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Pair with maximum GCD from two arrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.