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.
Input: N = 3, K = 2
Output : 3
No of binary strings:
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 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.
dp[i] = dp[i - 1] + dp[i - k]
Below is the implementation of the above approach:
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.
- 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 firstname.lastname@example.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.