Given two integers** N** and **K**, the task is to find the number of binary strings of length N having an even number of 1’s out of which less than K are consecutive.**Examples:**

Input:N = 4, K = 2Output:4Explanation:

The possible binary strings are 0000, 0101, 1001, 1010. They all have even number of 1’s with less than 2 of them occuring consecutively.

Input:N = 3, K = 2Output:2Explanation:

The possible binary strings are 000, 101. All other strings that is 001, 010, 011, 100, 110, 111 does not meet the criteria.

**Approach:**

This problem can be solved by Dynamic Programming.

Let us consider a 3D table **dp[][][] **to store the solution of each subproblem, such that, **dp[n][i][s]** denotes the number of binary strings of **length n** having **i consecutive 1’s** and **sum of 1’s = s**. As it is only required to check whether the total number of 1’s is even or not we store **s % 2**. So, **dp[n][i][s]** can be calculated as follows:

- If we place
**0**at the**n**, the number of 1’s remain unchanged. Hence,^{th}position**dp[n][i][s] = dp[n – 1][0][s]**. - If we place 1 at the
**n**,^{th}position**dp[n][i][s] = dp[n – 1][i + 1][(s + 1) % 2]**. - From the above two points the recurrence relation formed is given by:

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Table to store solution ` `// of each subproblem ` `int` `dp[100001][20][2]; ` ` ` `// Function to calculate ` `// the possible binary ` `// strings ` `int` `possibleBinaries(` `int` `pos, ` ` ` `int` `ones, ` ` ` `int` `sum, ` ` ` `int` `k) ` `{ ` ` ` `// If number of ones ` ` ` `// is equal to K ` ` ` `if` `(ones == k) ` ` ` `return` `0; ` ` ` ` ` `// pos: current position ` ` ` `// Base Case: When n ` ` ` `// length is traversed ` ` ` `if` `(pos == 0) ` ` ` ` ` `// sum: count of 1's ` ` ` `// Return the count ` ` ` `// of 1's obtained ` ` ` `return` `(sum == 0) ? 1 : 0; ` ` ` ` ` `// If the subproblem has already ` ` ` `// been solved ` ` ` `if` `(dp[pos][ones][sum] != -1) ` ` ` `// Return the answer ` ` ` `return` `dp[pos][ones][sum]; ` ` ` ` ` `// Recursive call when current ` ` ` `// position is filled with 1 ` ` ` `int` `ret = possibleBinaries(pos - 1, ` ` ` `ones + 1, ` ` ` `(sum + 1) % 2, ` ` ` `k) ` ` ` `// Recursive call when current ` ` ` `// position is filled with 0 ` ` ` `+ possibleBinaries(pos - 1, 0, ` ` ` `sum, k); ` ` ` ` ` `// Store the solution ` ` ` `// to this subproblem ` ` ` `dp[pos][ones][sum] = ret; ` ` ` ` ` `return` `dp[pos][ones][sum]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `K = 2; ` ` ` ` ` `// Initialising the ` ` ` `// table with -1 ` ` ` `memset` `(dp, -1, ` `sizeof` `dp); ` ` ` ` ` `cout << possibleBinaries(N, 0, 0, K); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Table to store solution ` `// of each subproblem ` `static` `int` `[][][]dp = ` `new` `int` `[` `100001` `][` `20` `][` `2` `]; ` ` ` `// Function to calculate ` `// the possible binary ` `// Strings ` `static` `int` `possibleBinaries(` `int` `pos, ` `int` `ones, ` ` ` `int` `sum, ` `int` `k) ` `{ ` ` ` ` ` `// If number of ones ` ` ` `// is equal to K ` ` ` `if` `(ones == k) ` ` ` `return` `0` `; ` ` ` ` ` `// pos: current position ` ` ` `// Base Case: When n ` ` ` `// length is traversed ` ` ` `if` `(pos == ` `0` `) ` ` ` ` ` `// sum: count of 1's ` ` ` `// Return the count ` ` ` `// of 1's obtained ` ` ` `return` `(sum == ` `0` `) ? ` `1` `: ` `0` `; ` ` ` ` ` `// If the subproblem has already ` ` ` `// been solved ` ` ` `if` `(dp[pos][ones][sum] != -` `1` `) ` ` ` ` ` `// Return the answer ` ` ` `return` `dp[pos][ones][sum]; ` ` ` ` ` `// Recursive call when current ` ` ` `// position is filled with 1 ` ` ` `int` `ret = possibleBinaries(pos - ` `1` `, ` ` ` `ones + ` `1` `, ` ` ` `(sum + ` `1` `) % ` `2` `, k) + ` ` ` ` ` `// Recursive call when current ` ` ` `// position is filled with 0 ` ` ` `possibleBinaries(pos - ` `1` `, ` `0` `, ` ` ` `sum, k); ` ` ` ` ` `// Store the solution ` ` ` `// to this subproblem ` ` ` `dp[pos][ones][sum] = ret; ` ` ` ` ` `return` `dp[pos][ones][sum]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `; ` ` ` `int` `K = ` `2` `; ` ` ` ` ` `// Initialising the ` ` ` `// table with -1 ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `100001` `; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `20` `; j++) ` ` ` `{ ` ` ` `for` `(` `int` `l = ` `0` `; l < ` `2` `; l++) ` ` ` `dp[i][j][l] = -` `1` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `System.out.print(possibleBinaries(N, ` `0` `, ` `0` `, K)); ` `} ` `} ` ` ` `// This code is contributed by Rohit_ranjan ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Table to store solution ` `// of each subproblem ` `static` `int` `[,,]dp = ` `new` `int` `[100001, 20, 2]; ` ` ` `// Function to calculate the ` `// possible binary Strings ` `static` `int` `possibleBinaries(` `int` `pos, ` `int` `ones, ` ` ` `int` `sum, ` `int` `k) ` `{ ` ` ` ` ` `// If number of ones ` ` ` `// is equal to K ` ` ` `if` `(ones == k) ` ` ` `return` `0; ` ` ` ` ` `// pos: current position ` ` ` `// Base Case: When n ` ` ` `// length is traversed ` ` ` `if` `(pos == 0) ` ` ` ` ` `// sum: count of 1's ` ` ` `// Return the count ` ` ` `// of 1's obtained ` ` ` `return` `(sum == 0) ? 1 : 0; ` ` ` ` ` `// If the subproblem has already ` ` ` `// been solved ` ` ` `if` `(dp[pos, ones, sum] != -1) ` ` ` ` ` `// Return the answer ` ` ` `return` `dp[pos, ones, sum]; ` ` ` ` ` `// Recursive call when current ` ` ` `// position is filled with 1 ` ` ` `int` `ret = possibleBinaries(pos - 1, ` ` ` `ones + 1, ` ` ` `(sum + 1) % 2, k) + ` ` ` `// Recursive call when current ` ` ` `// position is filled with 0 ` ` ` `possibleBinaries(pos - 1, 0, ` ` ` `sum, k); ` ` ` ` ` `// Store the solution ` ` ` `// to this subproblem ` ` ` `dp[pos, ones, sum] = ret; ` ` ` ` ` `return` `dp[pos, ones, sum]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `K = 2; ` ` ` ` ` `// Initialising the ` ` ` `// table with -1 ` ` ` `for` `(` `int` `i = 0; i < 100001; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < 20; j++) ` ` ` `{ ` ` ` `for` `(` `int` `l = 0; l < 2; l++) ` ` ` `dp[i, j, l] = -1; ` ` ` `} ` ` ` `} ` ` ` `Console.Write(possibleBinaries(N, 0, 0, K)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** *O(2*N*K)*

## Recommended Posts:

- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of binary strings of length N having equal count of 0's and 1's
- Count number of binary strings without consecutive 1’s : Set 2
- Count number of binary strings without consecutive 1's
- Count of binary strings of given length consisting of at least one 1
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Count strings with consecutive 1's
- Count of sub-strings with equal consecutive 0's and 1's
- Count of same length Strings that exists lexicographically in between two given Strings
- Count of sub-strings of length n possible from the given string
- Count the number of Special Strings of a given length N
- Count ways to increase LCS length of two strings by one
- Count of non-palindromic strings of length M using given N characters
- Count of strings whose prefix match with the given string to a given length k
- Maximum count of sub-strings of length K consisting of same characters
- Count of unique palindromic strings of length X from given string
- Count of Binary Strings possible as per given conditions
- Count binary strings with twice zeros in first half

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.