# Fibbinary Numbers (No consecutive 1s in binary) ŌĆō O(1) Approach

Fibbinary Numbers are numbers that have no consecutive 1s in their binary representation. However, they can have consecutive zeros in their binary representation. Binary representation is the representation in which the numbers are shown with base 2 and only two digits that are 1 and 0 only. Here, we will be given a number and have to find whether the given number is the fibbinary number or not.

Input 1: Given number: 10
Output: Yes


Explanation ŌłÆ The binary representation of the given number 10 is 1010 which shows that, there is no consecutive one in the binary form.

Input 2: Given number: 12
Output: No


Explanation ŌłÆ The binary representation of the given number is 1100, which shows that, there are two consecutive ones present in the binary form.

## Naive Approach

In this approach, we will use the division method to find each bit and will store the previous bit by dividing by 2 to get the required information. We will use the while loop until the current number becomes zero.

We will create a variable to store the previously founded bit and will initiate it will zero. If the current bit and the previous both are one then we will return false otherwise we will repeat until we will finish the loop.

After completing the loop, we will return true as there are no consecutive ones found. Let us see the code ŌłÆ

### Example

#include <iostream>
using namespace std;
bool isFibbinary(int n){
int temp = n; // defining the temporary number
int prev = 0; // defining the previous number
while(temp != 0){
// checking if the previous bit was zero or not
if(prev == 0){
// previous bit zero means no need to worry about current
prev = temp%2;
temp /= 2;
} else {
// if the previous bit was one and the current is also the same return false
if(temp%2 == 1){
return false;
} else {
prev = 0;
temp /=2;
}
}
}
// return true, as there is no consecutive ones present
return true;
}
// main function
int main(){
int n = 10; // given number
// calling to the function
if(isFibbinary(n)){
cout<<"The given number "<< n<< " is a Fibbinary Number"<<endl;
} else {
cout<<"The given number "<< n << " is not a Fibbnary Number"<<endl;
}
return 0;
}


### Output

The given number 10 is a Fibbinary Number


### Time and Space Complexity

The time complexity of the above code is O(log(N)), as we are dividing the current number by 2 until it becomes zero.

The space complexity of the above code is O(1), as we are not using any extra space here.

## Efficient Approach

In the previous approach, we have checked for each bit but there is another way to solve this problem which is shifting of the bits. As we know, in the Fibbinary numbers two consecutive bits are not one which means that if we shift all the bits by one then the bits of the previous number and the current number will never be the same at each position.

For example,

If we have the given number as 10 then its binary form will be 01010 and by shifting the bits by 1 we will get the number 10100 and we can see no 1 bit at the same position for both numbers.

This is the property of the fibbinary numbers that for the number n and left bit shifted n we have no bit same which makes their bit-and operator zero.

n & (n << 1) == 0


### Example

#include <iostream>
using namespace std;
bool isFibbinary(int n){
if((n & (n << 1)) == 0){
return true;
} else{
return false;
}
}
// main function
int main(){
int n = 12; // given number
// calling to the function
if(isFibbinary(n)){
cout<<"The given number "<< n<< " is a Fibbinary Number"<<endl;
} else {
cout<<"The given number "<< n << " is not a Fibbnary Number"<<endl;
}
return 0;
}


### Output

The given number 12 is not a Fibbnary Number


### Time and Space Complexity

The time complexity of the above code is O(1), as all the operations are done at the bit level and just two of them were there.

The space complexity of the above code is O(1), as we are not using any extra space here.

## Conclusion

In this tutorial, we have seen that the Fibbinary numbers are the numbers that have no consecutive 1s in their binary representation. However, they can have consecutive zeros in their binary representation. We have implemented two approaches here, one with the O(log(N)) time and O(1) space complexity with the divide by 2 methods and the second by using the property of the left-shift and bitwise-and operator.

Updated on: 16-May-2023

163 Views 