Given an integer **N**, the task is to find the smallest integer **X** such that it has no odd position set and **X ≥ N**.

**Note:** The positioning of bits is assumed from the right side and the first bit is assumed to be **0 ^{th}** bit.

**Examples:**

Input:N = 9

Output:16

16’s binary representation is 10000, which has its 4th bit

set which is the smallest number possible satisfying the given condition.

Input:N = 5

Output:5

Input:N = 19

Output:20

**Approach:** The problem can be solved using a greedy approach and some bit properties. The property that if smaller powers of two are taken **exactly once** and added up they can never exceed a higher power of two (e.g., (1 + 2 + 4) < 8). The following greedy approach is used to solve the above problem:

- Initially count the number of bits.
- Get the leftmost index of the set bit.
- If the leftmost set bit is at an odd index, then answer will always be
**(1 << (leftmost_bit_index + 1))**. - Else, greedily form a number by setting all the even bits from 0 to leftmost_bit_index. Now greedily remove a power of two from right to check if we get a number which satisfies the given conditions.
- If the above condition does not give us our number, then we simply set the next leftmost even bit and return the answer.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the total bits ` `int` `countBits(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Iterate and find the ` ` ` `// number of set bits ` ` ` `while` `(n) { ` ` ` `count++; ` ` ` ` ` `// Right shift the number by 1 ` ` ` `n >>= 1; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Function to find the nearest number ` `int` `findNearestNumber(` `int` `n) ` `{ ` ` ` ` ` `// Count the total number of bits ` ` ` `int` `cnt = countBits(n); ` ` ` ` ` `// To get the position ` ` ` `cnt -= 1; ` ` ` ` ` `// If the last set bit is ` ` ` `// at odd position then ` ` ` `// answer will always be a number ` ` ` `// with the left bit set ` ` ` `if` `(cnt % 2) { ` ` ` `return` `1 << (cnt + 1); ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` ` ` `int` `tempnum = 0; ` ` ` ` ` `// Set all the even bits which ` ` ` `// are possible ` ` ` `for` `(` `int` `i = 0; i <= cnt; i += 2) ` ` ` `tempnum += 1 << i; ` ` ` ` ` `// If the number still is less than N ` ` ` `if` `(tempnum < n) { ` ` ` ` ` `// Return the number by setting the ` ` ` `// next even set bit ` ` ` `return` `(1 << (cnt + 2)); ` ` ` `} ` ` ` ` ` `else` `if` `(tempnum == n) ` ` ` `return` `n; ` ` ` ` ` `// If we have reached this position ` ` ` `// it means tempsum > n ` ` ` `// hence turn off even bits to get the ` ` ` `// first possible number ` ` ` `for` `(` `int` `i = 0; i <= cnt; i += 2) { ` ` ` ` ` `// Turn off the bit ` ` ` `tempnum -= (1 << i); ` ` ` ` ` `// If it gets lower than N ` ` ` `// then set it and return that number ` ` ` `if` `(tempnum < n) ` ` ` `return` `tempnum += (1 << i); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 19; ` ` ` `cout << findNearestNumber(n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to count the total bits ` ` ` `static` `int` `countBits(` `int` `n) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Iterate and find the ` ` ` `// number of set bits ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `count++; ` ` ` ` ` `// Right shift the number by 1 ` ` ` `n >>= ` `1` `; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Function to find the nearest number ` ` ` `static` `int` `findNearestNumber(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Count the total number of bits ` ` ` `int` `cnt = countBits(n); ` ` ` ` ` `// To get the position ` ` ` `cnt -= ` `1` `; ` ` ` ` ` `// If the last set bit is ` ` ` `// at odd position then ` ` ` `// answer will always be a number ` ` ` `// with the left bit set ` ` ` `if` `(cnt % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `return` `1` `<< (cnt + ` `1` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `int` `tempnum = ` `0` `; ` ` ` ` ` `// Set all the even bits which ` ` ` `// are possible ` ` ` `for` `(` `int` `i = ` `0` `; i <= cnt; i += ` `2` `) ` ` ` `{ ` ` ` `tempnum += ` `1` `<< i; ` ` ` `} ` ` ` ` ` `// If the number still is less than N ` ` ` `if` `(tempnum < n) ` ` ` `{ ` ` ` ` ` `// Return the number by setting the ` ` ` `// next even set bit ` ` ` `return` `(` `1` `<< (cnt + ` `2` `)); ` ` ` `} ` ` ` `else` ` ` `if` `(tempnum == n) ` ` ` `{ ` ` ` `return` `n; ` ` ` `} ` ` ` ` ` `// If we have reached this position ` ` ` `// it means tempsum > n ` ` ` `// hence turn off even bits to get the ` ` ` `// first possible number ` ` ` `for` `(` `int` `i = ` `0` `; i <= cnt; i += ` `2` `) ` ` ` `{ ` ` ` ` ` `// Turn off the bit ` ` ` `tempnum -= (` `1` `<< i); ` ` ` ` ` `// If it gets lower than N ` ` ` `// then set it and return that number ` ` ` `if` `(tempnum < n) ` ` ` `{ ` ` ` `return` `tempnum += (` `1` `<< i); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `Integer.MIN_VALUE; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `19` `; ` ` ` ` ` `System.out.println(findNearestNumber(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to count the total bits ` ` ` `static` `int` `countBits(` `int` `n) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Iterate and find the ` ` ` `// number of set bits ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `count++; ` ` ` ` ` `// Right shift the number by 1 ` ` ` `n >>= 1; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Function to find the nearest number ` ` ` `static` `int` `findNearestNumber(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Count the total number of bits ` ` ` `int` `cnt = countBits(n); ` ` ` ` ` `// To get the position ` ` ` `cnt -= 1; ` ` ` ` ` `// If the last set bit is ` ` ` `// at odd position then ` ` ` `// answer will always be a number ` ` ` `// with the left bit set ` ` ` `if` `(cnt % 2 == 1) ` ` ` `{ ` ` ` `return` `1 << (cnt + 1); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `int` `tempnum = 0; ` ` ` ` ` `// Set all the even bits which ` ` ` `// are possible ` ` ` `for` `(` `int` `i = 0; i <= cnt; i += 2) ` ` ` `{ ` ` ` `tempnum += 1 << i; ` ` ` `} ` ` ` ` ` `// If the number still is less than N ` ` ` `if` `(tempnum < n) ` ` ` `{ ` ` ` ` ` `// Return the number by setting the ` ` ` `// next even set bit ` ` ` `return` `(1 << (cnt + 2)); ` ` ` `} ` ` ` `else` ` ` `if` `(tempnum == n) ` ` ` `{ ` ` ` `return` `n; ` ` ` `} ` ` ` ` ` `// If we have reached this position ` ` ` `// it means tempsum > n ` ` ` `// hence turn off even bits to get the ` ` ` `// first possible number ` ` ` `for` `(` `int` `i = 0; i <= cnt; i += 2) ` ` ` `{ ` ` ` ` ` `// Turn off the bit ` ` ` `tempnum -= (1 << i); ` ` ` ` ` `// If it gets lower than N ` ` ` `// then set it and return that number ` ` ` `if` `(tempnum < n) ` ` ` `{ ` ` ` `return` `tempnum += (1 << i); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `int` `.MinValue; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 19; ` ` ` ` ` `Console.WriteLine(findNearestNumber(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the above approach ` ` ` ` ` `# Function to count the total bits ` `def` `countBits(n): ` ` ` `count ` `=` `0` `; ` ` ` ` ` `# Iterate and find the ` ` ` `# number of set bits ` ` ` `while` `(n>` `0` `): ` ` ` `count` `+` `=` `1` `; ` ` ` ` ` `# Right shift the number by 1 ` ` ` `n >>` `=` `1` `; ` ` ` `return` `count; ` ` ` `# Function to find the nearest number ` `def` `findNearestNumber(n): ` ` ` ` ` `# Count the total number of bits ` ` ` `cnt ` `=` `countBits(n); ` ` ` ` ` `# To get the position ` ` ` `cnt ` `-` `=` `1` `; ` ` ` ` ` `# If the last set bit is ` ` ` `# at odd position then ` ` ` `# answer will always be a number ` ` ` `# with the left bit set ` ` ` `if` `(cnt ` `%` `2` `): ` ` ` `return` `1` `<< (cnt ` `+` `1` `); ` ` ` ` ` `else` `: ` ` ` ` ` `tempnum ` `=` `0` `; ` ` ` ` ` `# Set all the even bits which ` ` ` `# are possible ` ` ` `for` `i ` `in` `range` `(` `0` `,cnt` `+` `1` `,` `2` `): ` ` ` `tempnum ` `+` `=` `1` `<< i; ` ` ` ` ` `# If the number still is less than N ` ` ` `if` `(tempnum < n): ` ` ` ` ` `# Return the number by setting the ` ` ` `# next even set bit ` ` ` `return` `(` `1` `<< (cnt ` `+` `2` `)); ` ` ` ` ` `elif` `(tempnum ` `=` `=` `n): ` ` ` `return` `n; ` ` ` ` ` `# If we have reached this position ` ` ` `# it means tempsum > n ` ` ` `# hence turn off even bits to get the ` ` ` `# first possible number ` ` ` `for` `i ` `in` `range` `(` `0` `,cnt` `+` `1` `,` `2` `): ` ` ` ` ` `# Turn off the bit ` ` ` `tempnum ` `-` `=` `(` `1` `<< i); ` ` ` ` ` `# If it gets lower than N ` ` ` `# then set it and return that number ` ` ` `if` `(tempnum < n): ` ` ` `tempnum ` `+` `=` `(` `1` `<< i); ` ` ` `return` `tempnum; ` `# Driver code ` `n ` `=` `19` `; ` `print` `(findNearestNumber(n)); ` ` ` `# This code contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

20

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:

- Print odd positioned nodes of odd levels in level order of the given binary tree
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Check if a number has an odd count of odd divisors and even count of even divisors
- Find smallest number n such that n XOR n+1 equals to given k.
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Smallest integer greater than n such that it consists of digit m exactly k times
- Print even positioned nodes of even levels in level order of the given binary tree
- Next greater number than N with exactly one bit different in binary representation of N
- Smallest number divisible by n and has at-least k trailing zeros
- Smallest number whose square has N digits
- Find the number of pairs such that their gcd is equals to 1
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count of even and odd set bit with array element after XOR with K
- Count of even and odd set bit Array elements after XOR with K for Q queries

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.