
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Count Binary String without Consecutive 1's
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 to 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 a 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]
Input and Output
Input: This algorithm takes number of bits for a binary number. Let the input is 4. Output: 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)
Algorithm
countBinNums(n)
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
Example
#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 consecitive 1's: "<<countBinNums(n) << endl; return 0; }
Output
Enter number of bits: 4 Number of binary numbers without consecitive 1's: 8
- Related Articles
- Count number of binary strings without consecutive 1's in C
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Python Program to Count number of binary strings without consecutive 1’
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Count 1’s in a sorted binary array in C++
- Javascript Program to Count 1’s in a sorted binary array
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- Finding maximum number of consecutive 1's in a binary array in JavaScript
- C# program to check if there are K consecutive 1’s in a binary number
- Python program to check if there are K consecutive 1’s in a binary number?
- Maximum Consecutive Zeroes in Concatenated Binary String in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Count subarrays consisting of only 0’s and only 1’s in a binary array in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++

Advertisements