Given a positive integer **X** the task is to find an integer **Y** such that:

- The count of set bits is
**Y**is equal to the count of set bits in**X**. **X != Y**.**|X – Y|**is minimum.

**Examples:**

Input:X = 92

Output:90

90 is the closest number to 92 having

equal number of set bits.

Input:X = 17

Output:18

**Approach:** A little math can lead us to the solution approach. Since the number of bits in both the numbers has to be the same, if a set bit is flipped then an unset bit will also have to be flipped.

Now the problem reduces to choosing two bits for the flipping. Suppose one bit at index **i** is flipped and another bit at index **j** (j < i) from the LSB (least significant bit). Then the absolute value of the difference between the original integer and the new one is **2 ^{i} – 2^{j}**. To minimize this,

**i**has to be as small as possible and

**j**has to be as close to

**i**as possible.

Since the number of set bits have to be equal, so the bit at index

**i**must be different from the bit at index

**j**. This means that the smallest can be the rightmost bit that's different from the LSB, and

**j**must be the very next bit. In summary, the correct approach is to swap the two rightmost consecutive bits that are different.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `NumUnsignBits = 64; ` ` ` `// Function to return the number ` `// closest to x which has equal ` `// number of set bits as x ` `unsigned ` `long` `findNum(unsigned ` `long` `x) ` `{ ` ` ` `// Loop for each bit in x and ` ` ` `// compare with the next bit ` ` ` `for` `(` `int` `i = 0; i < NumUnsignBits - 1; i++) { ` ` ` `if` `(((x >> i) & 1) != ((x >> (i + 1)) & 1)) { ` ` ` `x ^= (1 << i) | (1 << (i + 1)); ` ` ` `return` `x; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 92; ` ` ` ` ` `cout << findNum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` `static` `int` `NumUnsignBits = ` `64` `; ` ` ` `// Function to return the number ` `// closest to x which has equal ` `// number of set bits as x ` `static` `long` `findNum(` `long` `x) ` `{ ` ` ` `// Loop for each bit in x and ` ` ` `// compare with the next bit ` ` ` `for` `(` `int` `i = ` `0` `; i < NumUnsignBits - ` `1` `; i++) ` ` ` `{ ` ` ` `if` `(((x >> i) & ` `1` `) != ((x >> (i + ` `1` `)) & ` `1` `)) ` ` ` `{ ` ` ` `x ^= (` `1` `<< i) | (` `1` `<< (i + ` `1` `)); ` ` ` `return` `x; ` ` ` `} ` ` ` `} ` ` ` `return` `Long.MIN_VALUE; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `92` `; ` ` ` ` ` `System.out.println(findNum(n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `NumUnsignBits ` `=` `64` `; ` ` ` `# Function to return the number ` `# closest to x which has equal ` `# number of set bits as x ` `def` `findNum(x) : ` ` ` ` ` `# Loop for each bit in x and ` ` ` `# compare with the next bit ` ` ` `for` `i ` `in` `range` `(NumUnsignBits ` `-` `1` `) : ` ` ` `if` `(((x >> i) & ` `1` `) !` `=` `((x >> (i ` `+` `1` `)) & ` `1` `)) : ` ` ` `x ^` `=` `(` `1` `<< i) | (` `1` `<< (i ` `+` `1` `)); ` ` ` `return` `x; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `92` `; ` ` ` `print` `(findNum(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `NumUnsignBits = 64; ` ` ` `// Function to return the number ` `// closest to x which has equal ` `// number of set bits as x ` `static` `long` `findNum(` `long` `x) ` `{ ` ` ` `// Loop for each bit in x and ` ` ` `// compare with the next bit ` ` ` `for` `(` `int` `i = 0; i < NumUnsignBits - 1; i++) ` ` ` `{ ` ` ` `if` `(((x >> i) & 1) != ((x >> (i + 1)) & 1)) ` ` ` `{ ` ` ` `x ^= (1 << i) | (1 << (i + 1)); ` ` ` `return` `x; ` ` ` `} ` ` ` `} ` ` ` `return` `long` `.MinValue; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 92; ` ` ` ` ` `Console.WriteLine(findNum(n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

90

**Time Complexity:** O(logn)

## Recommended Posts:

- Closest (or Next) smaller and greater numbers with same number of set bits
- Find the weight of the minimum spanning tree
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Minimum decrements to make integer A divisible by integer B
- Cyclic shifts of integer N by another integer m
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Find the closest and smaller tidy number
- Find the number closest to n and divisible by m
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find the multiple of x which is closest to a^b
- Queries to find the future closest date
- Find number from given list for which value of the function is closest to A
- Find a triplet in an array whose sum is closest to a given number
- Find the K closest points to origin using Priority Queue
- Find K Closest Points to the Origin
- Find the greater number closest to N having at most one non-zero digit
- Puzzle | Weight of Heavy Ball
- Number of N digit integers with weight W
- Number of Paths of Weight W in a K-ary tree

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.