# Number of ways to make binary string of length N such that 0s always occur together in groups of size K

Given two integers **N and K**, the task is to count the number of ways to make a binary string of length N such that 0s always occur together in a group of size K.

**Examples:**

Input:N = 3, K = 2

Output :3

No of binary strings:

111

100

001

Input :N = 4, K = 2

Output :5

This problem can easily be solved using **dynamic programming**. Let **dp[i]** be the number of binary strings of length **i** satisfying the condition.

From the condition it can be deduced that:

**dp[i] = 1 for 1 <= i < k**.- Also
**dp[k] = 2**since a binary string of length K will either be a string of only zeros or only ones. - Now if we consider for i > k. If we decide the i
^{th}character to be ‘1’, then**dp[i] = dp[i-1]**since the number of binary strings would not change. However if we decide the i^{th}character to be ‘0’, then we require that**previous k-1 characters should also be ‘0’ and hence dp[i] = dp[i-k]**. Therefore dp[i] will be the sum of these 2 values.

**Thus**,

dp[i] = dp[i - 1] + dp[i - k]

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `mod = 1000000007; ` ` ` `// Function to return no of ways to build a binary ` `// string of length N such that 0s always occur ` `// in groups of size K ` `int` `noOfBinaryStrings(` `int` `N, ` `int` `k) ` `{ ` ` ` `int` `dp[100002]; ` ` ` `for` `(` `int` `i = 1; i <= k - 1; i++) { ` ` ` `dp[i] = 1; ` ` ` `} ` ` ` ` ` `dp[k] = 2; ` ` ` ` ` `for` `(` `int` `i = k + 1; i <= N; i++) { ` ` ` `dp[i] = (dp[i - 1] + dp[i - k]) % mod; ` ` ` `} ` ` ` ` ` `return` `dp[N]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `int` `K = 2; ` ` ` `cout << noOfBinaryStrings(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `mod = ` `1000000007` `; ` ` ` `// Function to return no of ways to build a binary ` `// string of length N such that 0s always occur ` `// in groups of size K ` `static` `int` `noOfBinaryStrings(` `int` `N, ` `int` `k) ` `{ ` ` ` `int` `dp[] = ` `new` `int` `[` `100002` `]; ` ` ` `for` `(` `int` `i = ` `1` `; i <= k - ` `1` `; i++) ` ` ` `{ ` ` ` `dp[i] = ` `1` `; ` ` ` `} ` ` ` ` ` `dp[k] = ` `2` `; ` ` ` ` ` `for` `(` `int` `i = k + ` `1` `; i <= N; i++) ` ` ` `{ ` ` ` `dp[i] = (dp[i - ` `1` `] + dp[i - k]) % mod; ` ` ` `} ` ` ` ` ` `return` `dp[N]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `4` `; ` ` ` `int` `K = ` `2` `; ` ` ` `System.out.println(noOfBinaryStrings(N, K)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 iimplementation of the ` `# above approach ` `mod ` `=` `1000000007` `; ` ` ` `# Function to return no of ways to ` `# build a binary string of length N ` `# such that 0s always occur in ` `# groups of size K ` `def` `noOfBinaryStrings(N, k) : ` ` ` `dp ` `=` `[` `0` `] ` `*` `100002` `; ` ` ` `for` `i ` `in` `range` `(` `1` `, K) : ` ` ` `dp[i] ` `=` `1` `; ` ` ` ` ` `dp[k] ` `=` `2` `; ` ` ` ` ` `for` `i ` `in` `range` `(k ` `+` `1` `, N ` `+` `1` `) : ` ` ` `dp[i] ` `=` `(dp[i ` `-` `1` `] ` `+` `dp[i ` `-` `k]) ` `%` `mod; ` ` ` ` ` `return` `dp[N]; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `4` `; ` ` ` `K ` `=` `2` `; ` ` ` ` ` `print` `(noOfBinaryStrings(N, K)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `mod = 1000000007; ` ` ` `// Function to return no of ways to build ` `// a binary string of length N such that ` `// 0s always occur in groups of size K ` `static` `int` `noOfBinaryStrings(` `int` `N, ` `int` `k) ` `{ ` ` ` `int` `[]dp = ` `new` `int` `[100002]; ` ` ` `for` `(` `int` `i = 1; i <= k - 1; i++) ` ` ` `{ ` ` ` `dp[i] = 1; ` ` ` `} ` ` ` ` ` `dp[k] = 2; ` ` ` ` ` `for` `(` `int` `i = k + 1; i <= N; i++) ` ` ` `{ ` ` ` `dp[i] = (dp[i - 1] + dp[i - k]) % mod; ` ` ` `} ` ` ` ` ` `return` `dp[N]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 4; ` ` ` `int` `K = 2; ` ` ` `Console.WriteLine(noOfBinaryStrings(N, K)); ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## PHP

**Output:**

5

## Recommended Posts:

- Binary String of given length that without a palindrome of size 3
- Number of flips to make binary string alternate | Set 1
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Maximum number of groups of size 3 containing two type of items
- Ways to remove one element from a binary string so that XOR becomes zero
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Count of operations to make a binary string"ab" free
- Minimum swaps required to make a binary string alternating
- Ways to form a group from three groups with given constraints
- Check if a binary string contains all permutations of length k
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Length of longest consecutive ones by at most one swap in a Binary String
- Queries on number of Binary sub-matrices of Given size
- Minimum number of moves to make a binary array K periodic

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.