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

`<?php ` `// PHP implementation 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 ` `function` `noOfBinaryStrings(` `$N` `, ` `$k` `) ` `{ ` ` ` `global` `$mod` `; ` ` ` `$dp` `= ` `array` `(0, 100002, NULL); ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$k` `- 1; ` `$i` `++) ` ` ` `{ ` ` ` `$dp` `[` `$i` `] = 1; ` ` ` `} ` ` ` ` ` `$dp` `[` `$k` `] = 2; ` ` ` ` ` `for` `(` `$i` `= ` `$k` `+ 1; ` `$i` `<= ` `$N` `; ` `$i` `++) ` ` ` `{ ` ` ` `$dp` `[` `$i` `] = (` `$dp` `[` `$i` `- 1] + ` ` ` `$dp` `[` `$i` `- ` `$k` `]) % ` `$mod` `; ` ` ` `} ` ` ` ` ` `return` `$dp` `[` `$N` `]; ` `} ` ` ` `// Driver Code ` `$N` `= 4; ` `$K` `= 2; ` `echo` `noOfBinaryStrings(` `$N` `, ` `$K` `); ` ` ` `// This code is contributed by ita_c ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Minimum flips required to keep all 1s together in a Binary string
- Minimum jumps required to group all 1s together in a given Binary string
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Count the number of ways to divide N in k groups incrementally
- Print n 0s and m 1s such that no two 0s and no three 1s are together
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Number of ways to split a binary number such that every part is divisible by 2
- Ways to form a group from three groups with given constraints
- Binary String of given length that without a palindrome of size 3
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- Maximum size of square such that all submatrices of that size have sum less than K
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Number of binary strings such that there is no substring of length ≥ 3
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Number of ways to make exactly C components in a 2*N matrix
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Number of flips to make binary string alternate | Set 1

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.