# Find two integers A and B such that A ^ N = A + N and B ^ N = B + N

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**

**Examples:**

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:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MAX 32 ` ` ` `// Function to find A and B ` `void` `findAB(` `int` `N) ` `{ ` ` ` `bitset<MAX> arr(N), brr(N); ` ` ` ` ` `// To store the leftmost set bit in N ` ` ` `int` `leftsetN = -1; ` ` ` `for` `(` `int` `i = MAX - 1; i >= 0; --i) { ` ` ` `if` `(arr[i] == 1) { ` ` ` `leftsetN = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// To store the value of A ` ` ` `int` `A = 0; ` ` ` `for` `(` `int` `i = leftsetN; i >= 0; --i) { ` ` ` ` ` `// If the bit is unset in N ` ` ` `// then we will set it in A ` ` ` `if` `(arr[i] == 0) { ` ` ` `A |= (1 << i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// To store the value of B ` ` ` `int` `B = 0; ` ` ` ` ` `// B will be (1 << (leftsetN + 1)) ` ` ` `B = 1 << (leftsetN + 1); ` ` ` ` ` `// Print the values of A and B ` ` ` `cout << ` `"A = "` `<< A << ` `" and B = "` `<< B; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` ` ` `findAB(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

A = 2 and B = 8

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the prime P using given four integers
- Find N distinct integers with zero sum
- Find N distinct integers with sum N
- Find K consecutive integers such that their sum is N
- Find N integers with given difference between product and sum
- Find K distinct positive odd integers with sum N
- Find three integers less than or equal to N such that their LCM is maximum
- Find sum in range L to R in given sequence of integers
- Find if it is possible to choose subarray that it contains exactly K even integers
- Find the first N integers such that the sum of their digits is equal to 10
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Find any K distinct odd integers such that their sum is equal to N
- Find Prime Adam integers in the given range [L, R]
- Find n positive integers that satisfy the given equations
- Find distinct integers for a triplet with given product
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Find pair with maximum GCD for integers in range 2 to N
- Find a pair with maximum product in array of Integers
- Find the integers that doesnot ends with T1 or T2 when squared and added X
- Queries to find the count of integers in a range that contain the given pattern

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.