# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ith character to be ‘1’, then dp[i] = dp[i-1] since the number of binary strings would not change. However if we decide the ith 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 ` `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; ` `    ``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; ` `} `

## 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 `

## 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 `

## 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``; ` `    ``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 */`

## PHP

 ` `

Output:

```5
```

My Personal Notes arrow_drop_up An enthusiastic Java and web developer with a little affinity for tea, cricket, English, etymology, and reading

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.