# Count binary strings with k times appearing adjacent two set bits

Given two integers n and k, count the number of binary strings of length n with k as number of times adjacent 1’s appear.

**Examples:**

Input : n = 5, k = 2 Output : 6 Explanation: Binary strings of length 5 in which k number of times two adjacent set bits appear. 001110111011100110111011111101 Input : n = 4, k = 1 Output : 3 Explanation: Binary strings of length 3 in which k number of times two adjacent set bits appear. 00111100 0110

Lets try writing the recursive function for the above problem statement:

1) n = 1, only two binary strings exist with length 1, not having any adjacent 1’s

String 1 : “0”

String 2 : “1”

2) For all n > 1 and all k, two cases arise

a) Strings ending with 0 : String of length n can be created by appending 0 to all **strings of length n-1 having k times two adjacent 1’s ending with both 0 and 1** (Having 0 at n’th position will not change the count of adjacent 1’s).

b) Strings ending with 1 : String of length n can be created by appending 1 to all strings of **length n-1 having k times adjacent 1’s and ending with 0** and to all strings of **length n-1 having k-1 adjacent 1’s and ending with 1**.

Example: let s = 011 i.e. a string ending with 1 having adjacent count as 1. Adding 1 to it, s = 0111 increase the count of adjacent 1.

Let there be an array dp[i][j][2] wheredp[i][j][0]denotes number of binary strings with length i having j number of two adjacent 1's and ending with 0. Similarlydp[i][j][1]denotes the same binary strings with length i and j adjacent 1's but ending with 1. Then: dp[1][0][0] = 1 and dp[1][0][1] = 1 For all other i and j, dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1] dp[i][j][1] = dp[i-1][j][0] + dp[i-1][j-1][1] Then, output dp[n][k][0] + dp[n][k][1]

## C++

`// C++ program to count number of binary strings ` `// with k times appearing consecutive 1's. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countStrings(` `int` `n, ` `int` `k) ` `{ ` ` ` `// dp[i][j][0] stores count of binary ` ` ` `// strings of length i with j consecutive ` ` ` `// 1's and ending at 0. ` ` ` `// dp[i][j][1] stores count of binary ` ` ` `// strings of length i with j consecutive ` ` ` `// 1's and ending at 1. ` ` ` `int` `dp[n + 1][k + 1][2]; ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `// If n = 1 and k = 0. ` ` ` `dp[1][0][0] = 1; ` ` ` `dp[1][0][1] = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` ` ` `// number of adjacent 1's can not exceed i-1 ` ` ` `for` `(` `int` `j = 0; j <= k; j++) { ` ` ` `dp[i][j][0] = dp[i - 1][j][0] + dp[i - 1][j][1]; ` ` ` `dp[i][j][1] = dp[i - 1][j][0]; ` ` ` ` ` `if` `(j - 1 >= 0) ` ` ` `dp[i][j][1] += dp[i - 1][j - 1][1]; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `dp[n][k][0] + dp[n][k][1]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, k = 2; ` ` ` `cout << countStrings(n, k); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of binary strings ` `// with k times appearing consecutive 1's. ` `class` `GFG { ` ` ` ` ` `static` `int` `countStrings(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// dp[i][j][0] stores count of binary ` ` ` `// strings of length i with j consecutive ` ` ` `// 1's and ending at 0. ` ` ` `// dp[i][j][1] stores count of binary ` ` ` `// strings of length i with j consecutive ` ` ` `// 1's and ending at 1. ` ` ` `int` `dp[][][] = ` `new` `int` `[n + ` `1` `][k + ` `1` `][` `2` `]; ` ` ` ` ` `// If n = 1 and k = 0. ` ` ` `dp[` `1` `][` `0` `][` `0` `] = ` `1` `; ` ` ` `dp[` `1` `][` `0` `][` `1` `] = ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) { ` ` ` ` ` `// number of adjacent 1's can not exceed i-1 ` ` ` `for` `(` `int` `j = ` `0` `; j < i && j < k + ` `1` `; j++) { ` ` ` `dp[i][j][` `0` `] = dp[i - ` `1` `][j][` `0` `] + dp[i - ` `1` `][j][` `1` `]; ` ` ` `dp[i][j][` `1` `] = dp[i - ` `1` `][j][` `0` `]; ` ` ` ` ` `if` `(j - ` `1` `>= ` `0` `) { ` ` ` `dp[i][j][` `1` `] += dp[i - ` `1` `][j - ` `1` `][` `1` `]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `dp[n][k][` `0` `] + dp[n][k][` `1` `]; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `, k = ` `2` `; ` ` ` `System.out.println(countStrings(n, k)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number of ` `# binary strings with k times appearing ` `# consecutive 1's. ` `def` `countStrings(n, k): ` ` ` ` ` `# dp[i][j][0] stores count of binary ` ` ` `# strings of length i with j consecutive ` ` ` `# 1's and ending at 0. ` ` ` `# dp[i][j][1] stores count of binary ` ` ` `# strings of length i with j consecutive ` ` ` `# 1's and ending at 1. ` ` ` `dp ` `=` `[[[` `0` `, ` `0` `] ` `for` `__ ` `in` `range` `(k ` `+` `1` `)] ` ` ` `for` `_ ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `# If n = 1 and k = 0. ` ` ` `dp[` `1` `][` `0` `][` `0` `] ` `=` `1` ` ` `dp[` `1` `][` `0` `][` `1` `] ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` ` ` `# number of adjacent 1's can not exceed i-1 ` ` ` `for` `j ` `in` `range` `(k ` `+` `1` `): ` ` ` `dp[i][j][` `0` `] ` `=` `(dp[i ` `-` `1` `][j][` `0` `] ` `+` ` ` `dp[i ` `-` `1` `][j][` `1` `]) ` ` ` `dp[i][j][` `1` `] ` `=` `dp[i ` `-` `1` `][j][` `0` `] ` ` ` `if` `j >` `=` `1` `: ` ` ` `dp[i][j][` `1` `] ` `+` `=` `dp[i ` `-` `1` `][j ` `-` `1` `][` `1` `] ` ` ` ` ` `return` `dp[n][k][` `0` `] ` `+` `dp[n][k][` `1` `] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `5` ` ` `k ` `=` `2` ` ` `print` `(countStrings(n, k)) ` ` ` `# This code is contributed by vibhu4agarwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of binary strings ` `// with k times appearing consecutive 1's. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `countStrings(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// dp[i][j][0] stores count of binary ` ` ` `// strings of length i with j consecutive ` ` ` `// 1's and ending at 0. ` ` ` `// dp[i][j][1] stores count of binary ` ` ` `// strings of length i with j consecutive ` ` ` `// 1's and ending at 1. ` ` ` `int` `[,, ] dp = ` `new` `int` `[n + 1, k + 1, 2]; ` ` ` ` ` `// If n = 1 and k = 0. ` ` ` `dp[1, 0, 0] = 1; ` ` ` `dp[1, 0, 1] = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` ` ` `// number of adjacent 1's can not exceed i-1 ` ` ` `for` `(` `int` `j = 0; j < i && j < k + 1; j++) { ` ` ` `dp[i, j, 0] = dp[i - 1, j, 0] + dp[i - 1, j, 1]; ` ` ` `dp[i, j, 1] = dp[i - 1, j, 0]; ` ` ` ` ` `if` `(j - 1 >= 0) { ` ` ` `dp[i, j, 1] += dp[i - 1, j - 1, 1]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `dp[n, k, 0] + dp[n, k, 1]; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `n = 5, k = 2; ` ` ` `Console.WriteLine(countStrings(n, k)); ` ` ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number of binary strings ` `// with k times appearing consecutive 1's. ` ` ` `function` `countStrings(` `$n` `, ` `$k` `) ` `{ ` ` ` `// dp[i][j][0] stores count of binary ` ` ` `// strings of length i with j consecutive ` ` ` `// 1's and ending at 0. ` ` ` `// dp[i][j][1] stores count of binary ` ` ` `// strings of length i with j consecutive ` ` ` `// 1's and ending at 1. ` ` ` `$dp` `=` `array_fill` `(0, ` `$n` `+ 1, ` `array_fill` `(0, ` `$k` `+ 1, ` `array_fill` `(0, 2, 0))); ` ` ` ` ` `// If n = 1 and k = 0. ` ` ` `$dp` `[1][0][0] = 1; ` ` ` `$dp` `[1][0][1] = 1; ` ` ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `// number of adjacent 1's can not exceed i-1 ` ` ` `for` `(` `$j` `= 0; ` `$j` `< ` `$i` `; ` `$j` `++) ` ` ` `{ ` ` ` `if` `(isset(` `$dp` `[` `$i` `][` `$j` `][0])||isset(` `$dp` `[` `$i` `][` `$j` `][1])){ ` ` ` `$dp` `[` `$i` `][` `$j` `][0] = ` `$dp` `[` `$i` `- 1][` `$j` `][0] + ` `$dp` `[` `$i` `- 1][` `$j` `][1]; ` ` ` `$dp` `[` `$i` `][` `$j` `][1] = ` `$dp` `[` `$i` `- 1][` `$j` `][0]; ` ` ` `} ` ` ` `if` `(` `$j` `- 1 >= 0 && isset(` `$dp` `[` `$i` `][` `$j` `][1])) ` ` ` `$dp` `[` `$i` `][` `$j` `][1] += ` `$dp` `[` `$i` `- 1][` `$j` `- 1][1]; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `$dp` `[` `$n` `][` `$k` `][0] + ` `$dp` `[` `$n` `][` `$k` `][1]; ` `} ` ` ` `// Driver code ` `$n` `=5; ` `$k` `=2; ` `echo` `countStrings(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity :** O(n^{2})

This article is contributed by **Ekta Goel**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Number of Binary Strings of length N with K adjacent Set Bits
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Generate all the binary strings of N bits
- Count of strings where adjacent characters are of difference one
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count even length binary sequences with same sum of first and second half bits
- Maximum number of set bits count in a K-size substring of a Binary String
- Count of binary strings of given length consisting of at least one 1
- Count number of binary strings without consecutive 1's
- Count binary strings with twice zeros in first half
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
- Count number of binary strings without consecutive 1’s : Set 2
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- Count of distinct XORs formed by rearranging two Binary strings
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of subsets not containing adjacent elements
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- Minimum adjacent swaps required to Sort Binary array