Given N, check if the number is Fibbinary Number or not. Fibbinary numbers are integers whose binary representation contains no consecutive ones.
Input : 10 Output : YES Explanation: 1010 is the binary representation of 10 which does not contains any consecutive 1's. Input : 11 Output : NO Explanation: 1011 is the binary representation of 11, which contains consecutive 1's
The idea to do this is to right shift the number, till n!=0. For every binary representation of 1, check if the last bit found was 1 or not. Get the last bit of binary representation of the integer by doing a (n&1). If the last bit of the binary representation is 1 and the previous bit before doing a right shift was also one, we encounter consecutive 1’s. So we come to the conclusion that it is not a fibonnary number.
Some of the first few Fibonnary numbers are:
0, 2, 4, 8, 10, 16, 18, 20.......
Time Complexity: O( log(n) )
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.
- Fibbinary Numbers (No consecutive 1s in binary) - O(1) Approach
- 1 to n bit numbers with no consecutive 1s in binary representation.
- 1 to n bit numbers with no consecutive 1s in binary representation
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Count number of binary strings without consecutive 1’s : Set 2
- Length of the Longest Consecutive 1s in Binary Representation
- Find consecutive 1s of length >= n in binary representation of a number
- Find next greater element with no consecutive 1 in it's binary representation
- Express a number as sum of consecutive numbers
- Expressing factorial n as sum of consecutive numbers
- Sum of Hamming difference of consecutive numbers from 0 to N | Set 2
- Maximum number of consecutive 1's in binary representation of all the array elements
- Binary string with given frequencies of sums of consecutive pairs of characters
- Length of longest consecutive zeroes in the binary representation of a number.
- Prove that atleast one of three consecutive even numbers is divisible by 6
- Find all ranges of consecutive numbers from Array
- Find the number of consecutive zero at the end after multiplying n numbers
- Check if a number can be expressed as a sum of consecutive numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : Mithun Kumar