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

• Last Updated : 15 Jun, 2022

Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s.

Examples:

```Input:  N = 2
Output: 3
// The 3 strings are 00, 01, 10

Input: N = 3
Output: 5
// The 5 strings are 000, 001, 010, 100, 101```

## C++

 `// C++ program to count all distinct binary strings``// without two consecutive 1's``#include ``using` `namespace` `std;` `int` `countStrings(``int` `n)``{``    ``int` `a[n], b[n];``    ``a = b = 1;``    ``for` `(``int` `i = 1; i < n; i++) {``        ``a[i] = a[i - 1] + b[i - 1];``        ``b[i] = a[i - 1];``    ``}``    ``return` `a[n - 1] + b[n - 1];``}` `// Driver program to test above functions``int` `main()``{``    ``cout << countStrings(3) << endl;``    ``return` `0;``}`

Output:

```5
```

Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(n), where n represents the given integer.

Please refer complete article on Count number of binary strings without consecutive 1’s for more details!

My Personal Notes arrow_drop_up