C/C++ Program to Count number of binary strings without consecutive 1’s?



In this article, we will learn how to count all distinct binary strings of length n such that no two 1's appear consecutively. We'll explore this problem using both recursive and dynamic programming approaches in C and C++.

What is a Binary String?

A binary string is a sequence of characters that contains only '0' and '1'. It represents information in base-2 format. For example, "0101" is a binary string of length 4.

We are given a positive integer n, and our task is to count all possible distinct binary strings of length n that do not contain consecutive 1's.

Consider the following example scenarios to understand the problem better:

Scenario 1

Input: n = 3
Output: 5

Explanation:
Valid strings: {"000", "001", "010", "100", "101"}

Scenario 2

Input: n = 4
Output: 8

Explanation:
Valid strings: {"0000", "0001", "0010", "0100", "0101", "1000", "1001", "1010"}

Counting number of binary strings without consecutive 1's

To solve this problem, there are the following approaches:

Let us learn each approach in detail with the help of examples.

Using Recursive Solution

In this approach, we will use recursion to build binary strings step-by-step while making sure that no two consecutive 1's appear.

At each step, we can either place a '0' or a '1'. If the last placed bit is '0', we can safely place either '0' or '1' in the next position. But if the last placed bit is '1', we must place '0' next to avoid consecutive 1's. Here the following recurrence relation:

count_strings(i) = count_strings(i+1) + count_strings(i+2)

Example

The following C and C++ examples demonstrate how to count the number of binary strings of length n that do not contain consecutive 1's using recursion:

C++ C
#include <iostream>
using namespace std;
int count_strings(int n, int last_digit) {
   if (n == 0) return 0;
   if (n == 1) return last_digit ? 1 : 2;

   if (last_digit == 0)
      return count_strings(n - 1, 0) + count_strings(n - 1, 1);
   else
      return count_strings(n - 1, 0);
}

int main() {
   int n = 4;
   cout << "Number of " << n << "-digit binary strings without any consecutive 1's are " << count_strings(n, 0);
   return 0;
}

Output

The above program produces the following result:

Number of 4-digit binary strings without any consecutive 1's are 8
#include <stdio.h>
int count_strings(int n, int last_digit) {
   if (n == 0) return 0;
   if (n == 1) return last_digit ? 1 : 2;

   if (last_digit == 0)
      return count_strings(n - 1, 0) + count_strings(n - 1, 1);
   else
      return count_strings(n - 1, 0);
}

int main() {
   int n = 4;
   printf("Number of %d-digit binary strings without any consecutive 1's is %d\n",
          n, count_strings(n, 0));
   return 0;
}

Output

The above program produces the following result:

Number of 4-digit binary strings without any consecutive 1's are 8

Time and Space Complexity

Time Complexity: O(2n)
Space Complexity: O(n) - due to recursion stack.

Using Bottom-Up Dynamic Programming (Tabulation)

We use a DP array where dp[i] stores the count of valid binary strings starting from index i. The recurrence is the same as in recursion:

dp[i] = dp[i+1] + dp[i+2]

Example

The following C and C++ examples demonstrate how to count the number of binary strings of length n without consecutive 1's using dynamic programming:

C++ C
#include <bits/stdc++.h>
using namespace std;
int count_strings(int n) {
   if (n == 1) return 2;
   if (n == 2) return 3;

   vector<int> dp(n);
   dp[n - 1] = 2;
   dp[n - 2] = 3;

   for (int i = n - 3; i >= 0; i--) {
      dp[i] = dp[i + 1] + dp[i + 2];
   }

   return dp[0];
}
int main() {
   int n = 3;
   cout << "Number of " << n << "-digit binary strings without any consecutive 1's are "
        << count_strings(n);
   return 0;
}

Output

The above program produces the following result:

Number of 3-digit binary strings without any consecutive 1's are 5
#include <stdio.h>
#include <stdlib.h>
int count_strings(int n) {
   if (n == 1) return 2;
   if (n == 2) return 3;

   int* dp = (int*)malloc(n * sizeof(int));
   dp[n - 1] = 2;
   dp[n - 2] = 3;

   for (int i = n - 3; i >= 0; i--) {
      dp[i] = dp[i + 1] + dp[i + 2];
   }

   int result = dp[0];
   free(dp);
   return result;
}

int main() {
   int n = 3;
   printf("Number of %d-digit binary strings without any consecutive 1's are %d\n",
          n, count_strings(n));
   return 0;
}

Output

The above program produces the following result:

Number of 3-digit binary strings without any consecutive 1's are 5

Time and Space Complexity

Time Complexity: O(n)
Space Complexity: O(n)

Updated on: 2025-08-04T18:35:51+05:30

313 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements