Given a positive integer n. The problem is to check if the number is Number or not. are integers whose representation contains no ones.


Input : 03;
Output : Yes3;
Explanation: 010 is the binary representation 3;
             of 0 which does not contains any 3;
             consecutive 's.3;
Input : 13;
Output : No3;
Explanation: 01 is the binary representation 3;
             of 1, which contains consecutive 3;

Approach: If (n & (n >> 1)) == 0, then ‘n’ is a fibbinary number Else not.

// C++ implementation to check whether a number
// is fibbinary or not
#include <bits/stdc++.h>
using namespace std;

// function to check whether a number
// is fibbinary or not
bool isFibbinaryNum(unsigned int n) {

  // if the number does not contain adjacent ones
  // then (n & (n >> 1)) operation results to 0
  if ((n & (n >> 1)) == 0)
    return true;

  // not a fibbinary number
  return false;

// Driver program to test above
int main() {
  unsigned int n = 10;
  if (isFibbinaryNum(n))
    cout << "Yes";
    cout << "No";
  return 0;



Complexity: O(1).

- avatar - Fibbinary Numbers (No consecutive 1s in binary) &#8211; O(1) Approach

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at to report any issue with the above content.

Source link


Please enter your comment!
Please enter your name here