Given **Q** queries where each query consists of an integer **N** and the task is to find the smallest integer greater than **N** such that there are no consecutive **1s** in its binary representation.

**Examples:**

Input:Q[] = {4, 6}

Output:

5

8

Input:Q[] = {50, 23, 456}

Output:

64

32

512

**Approach:** Store all the numbers in a list whose binary representation does not contain consecutive 1s upto a fixed limit. Now for every given **N**, find the next greater element in the list generated previously using binary search.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MAX = 100000; ` ` ` `// To store the pre-computed integers ` `vector<` `int` `> v; ` ` ` `// Function that returns true if the ` `// binary representation of x contains ` `// consecutive 1s ` `int` `consecutiveOnes(` `int` `x) ` `{ ` ` ` ` ` `// To store the previous bit ` ` ` `int` `p = 0; ` ` ` `while` `(x > 0) { ` ` ` ` ` `// Check whether the previous bit ` ` ` `// and the current bit are both 1 ` ` ` `if` `(x % 2 == 1 and p == 1) ` ` ` `return` `true` `; ` ` ` ` ` `// Update previous bit ` ` ` `p = x % 2; ` ` ` ` ` `// Go to the next bit ` ` ` `x /= 2; ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `// Function to pre-compute the ` `// valid numbers from 0 to MAX ` `void` `preCompute() ` `{ ` ` ` `// Store all the numbers which do ` ` ` `// not have consecutive 1s ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) { ` ` ` `if` `(!consecutiveOnes(i)) ` ` ` `v.push_back(i); ` ` ` `} ` `} ` ` ` `// Function to return the minimum ` `// number greater than n which does ` `// not contain consecutive 1s ` `int` `nextValid(` `int` `n) ` `{ ` ` ` `// Search for the next greater element ` ` ` `// with no consecutive 1s ` ` ` `int` `it = upper_bound(v.begin(), ` ` ` `v.end(), n) ` ` ` `- v.begin(); ` ` ` `int` `val = v[it]; ` ` ` `return` `val; ` `} ` ` ` `// Function to perform the queries ` `void` `performQueries(` `int` `queries[], ` `int` `q) ` `{ ` ` ` `for` `(` `int` `i = 0; i < q; i++) ` ` ` `cout << nextValid(queries[i]) << ` `"\n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `queries[] = { 4, 6 }; ` ` ` `int` `q = ` `sizeof` `(queries) / ` `sizeof` `(` `int` `); ` ` ` ` ` `// Pre-compute the numbers ` ` ` `preCompute(); ` ` ` ` ` `// Perform the queries ` ` ` `performQueries(queries, q); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `bisect ` `import` `bisect_right as upper_bound ` ` ` `MAX` `=` `100000` ` ` `# To store the pre-computed integers ` `v ` `=` `[] ` ` ` `# Function that returns true if the ` `# binary representation of x contains ` `# consecutive 1s ` `def` `consecutiveOnes(x): ` ` ` ` ` `# To store the previous bit ` ` ` `p ` `=` `0` ` ` `while` `(x > ` `0` `): ` ` ` ` ` `# Check whether the previous bit ` ` ` `# and the current bit are both 1 ` ` ` `if` `(x ` `%` `2` `=` `=` `1` `and` `p ` `=` `=` `1` `): ` ` ` `return` `True` ` ` ` ` `# Update previous bit ` ` ` `p ` `=` `x ` `%` `2` ` ` ` ` `# Go to the next bit ` ` ` `x ` `/` `/` `=` `2` ` ` ` ` `return` `False` ` ` `# Function to pre-compute the ` `# valid numbers from 0 to MAX ` `def` `preCompute(): ` ` ` ` ` `# Store all the numbers which do ` ` ` `# not have consecutive 1s ` ` ` `for` `i ` `in` `range` `(` `MAX` `+` `1` `): ` ` ` `if` `(consecutiveOnes(i) ` `=` `=` `0` `): ` ` ` `v.append(i) ` ` ` `# Function to return the minimum ` `# number greater than n which does ` `# not contain consecutive 1s ` `def` `nextValid(n): ` ` ` ` ` `# Search for the next greater element ` ` ` `# with no consecutive 1s ` ` ` `it ` `=` `upper_bound(v, n) ` ` ` `val ` `=` `v[it] ` ` ` `return` `val ` ` ` `# Function to perform the queries ` `def` `performQueries(queries, q): ` ` ` `for` `i ` `in` `range` `(q): ` ` ` `print` `(nextValid(queries[i])) ` ` ` `# Driver code ` `queries ` `=` `[` `4` `, ` `6` `] ` `q ` `=` `len` `(queries) ` ` ` `# Pre-compute the numbers ` `preCompute() ` ` ` `# Perform the queries ` `performQueries(queries, q) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

5 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.

## Recommended Posts:

- Next greater number than N with exactly one bit different in binary representation of N
- Binary representation of next number
- Length of the Longest Consecutive 1s in Binary Representation
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Maximum number of consecutive 1's in binary representation of all the array elements
- Elements greater than the previous and next element in an Array
- Distance from Next Greater element
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Find next greater number with same set of digits
- Find the Next perfect square greater than a given number
- Find the next Factorial greater than N
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Maximum element in an array such that its previous and next element product is maximum
- Next greater Number than N with the same quantity of digits A and B
- Count of Array elements greater than all elements on its left and next K elements on its right
- Find longest sequence of 1's in binary representation with one flip
- Find the number obtained after concatenation of binary representation of M and N
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Replace two consecutive equal values with one greater

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.