- Related Questions & Answers
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Count numbers have all 1s together in binary representation in C++
- Find the number of binary strings of length N with at least 3 consecutive 1s in C++
- XOR counts of 0s and 1s in binary representation in C++
- N consecutive odd numbers JavaScript
- An Interesting Method to Generate Binary Numbers from 1 to n?
- Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program
- Binary String With Substrings Representing 1 To N in C++
- Count Binary String without Consecutive 1's
- C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- Java program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Count ways to reach a score using 1 and 2 with no consecutive 2s in C++
- Count of numbers having only 1 set bit in the range [0, n] in C++
- Find all combinations of k-bit numbers with n bits set where 1 <= n <= k in sorted order in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we have to find some binary numbers which have no consecutive 1s. In a 3-bit binary string, there are three binary numbers 011, 110, 111, who have consecutive 1s, and five numbers are there which have no consecutive 1s. So after applying this algorithm for 3-bit numbers, the answer will be 5.

If a[i] be the set of binary numbers, whose number of bits are i, and not containing any consecutive 1s, and b[i] is the set of binary number, where number of bits are i, and containing consecutive 1s, then there are recurrence relations like −

a[i] := a[i - 1] + b[i - 1]

b[i] := a[i - 1]

This algorithm takes number of bits for a binary number. Let the input is 4.

It returns the number of binary strings which have no consecutive 1’s.

Here the result is − 8. (There are 8 binary strings which has no consecutive 1’s)

Input: n is the number of bits. Output: Count how many numbers are present which have no consecutive 1. Begin define lists with strings ending with 0 and ending with 1 endWithZero[0] := 1 endWithOne[0] := 1 for i := 1 to n-1, do endWithZero[i] := endWithZero[i-1] + endWithOne[i-1] endWithOne[i] := endWithZero[i-1] done return endWithZero[n-1] + endWithOne[n-1] End

#include <iostream> using namespace std; int countBinNums(int n) { int endWithZero[n], endWithOne[n]; endWithZero[0] = endWithOne[0] = 1; for (int i = 1; i < n; i++) { endWithZero[i] = endWithZero[i-1] + endWithOne[i-1]; endWithOne[i] = endWithZero[i-1]; } return endWithZero[n-1] + endWithOne[n-1]; } int main(){ int n; cout << "Enter number of bits: "; cin >> n; cout << "Number of binary numbers without consecutive 1's: "<<countBinNums(n) << endl; return 0; }

Enter number of bits: 4 Number of binary numbers without consecutive 1's: 8

Advertisements