Given a non-negative integer N, the task is to find two integers A (greatest integer smaller than N) and (smallest integer greater than N) such that A + N = A ^ N and B + N = B ^ N
Input: N = 5
Output: A = 2 and B = 8
2 + 8 = 2 ^ 8 = 10
Input: N = 10
Output: A = 5 and B = 16
5 + 16 = 5 ^ 16 = 21
Approach: Lets find A and B independently. To solve this problem we have to use the property, x + y = x^y + 2 * (x & y)
Since the problem states that xor sum is equal to the given sum which implies that their AND must be 0.
- Finding A: N can be represented as a series of bits of 0 and 1. To find A we will first have to find the most significant bit of N which is set. Since A & N = 0, The places where N has set bit, for that places we will make bits of A as unset and for the places where N has unset bit, we will make that bit set for A as we want to maximize A. This we will do for all the bits from most significant to the least significant. Hence we will get our A.
- Finding B: Finding B is easy. Let i be the position of the leftmost set bit in 1. We want B to be greater than N, also we want B & N =0. Hence using these two facts B will be always (1<< (i+1)).
Below is the implementation of the above approach:
A = 2 and B = 8
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.
- Find two co-prime integers such that the first divides A and the second divides B
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Add two integers of different base and represent sum in smaller base of the two
- Find three integers less than or equal to N such that their LCM is maximum
- Find the first N integers such that the sum of their digits is equal to 10
- Find any K distinct odd integers such that their sum is equal to N
- Find K consecutive integers such that their sum is N
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Count possible splits of sum N into K integers such that the minimum is at least P
- Minimum number of integers required such that each Segment contains at least one of them
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Divide two integers without using multiplication, division and mod operator
- Divide two integers without using multiplication, division and mod operator | Set2
- Split N into two integers whose addition to A and B makes them equal
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.