Open In App

Count binary strings with k times appearing adjacent two set bits

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers n and k, count the number of binary strings of length n with k as number of times adjacent 1’s appear.

Examples: 

Input  : n = 5, k = 2
Output : 6
Explanation:
Binary strings of length 5 in which k number of times
two adjacent set bits appear.
00111  
01110
11100
11011
10111
11101

Input  : n = 4, k = 1
Output : 3
Explanation:
Binary strings of length 3 in which k number of times
two adjacent set bits appear.
0011  
1100
0110
Recommended Practice

Lets try writing the recursive function for the above problem statement: 
1) n = 1, only two binary strings exist with length 1, not having any adjacent 1’s 
      String 1 : “0” 
      String 2 : “1”
2) For all n > 1 and all k, two cases arise 
      a) Strings ending with 0 : String of length n can be created by appending 0 to all strings of length n-1 having k times two adjacent 1’s ending with both 0 and 1 (Having 0 at n’th position will not change the count of adjacent 1’s). 
      b) Strings ending with 1 : String of length n can be created by appending 1 to all strings of length n-1 having k times adjacent 1’s and ending with 0 and to all strings of length n-1 having k-1 adjacent 1’s and ending with 1.

Example: let s = 011 i.e. a string ending with 1 having adjacent count as 1. Adding 1 to it, s = 0111 increase the count of adjacent 1.  

Let there be an array dp[i][j][2] where dp[i][j][0]
denotes number of binary strings with length i having
j number of two adjacent 1's and ending with 0.
Similarly dp[i][j][1] denotes the same binary strings
with length i and j adjacent 1's but ending with 1.
Then: 
    dp[1][0][0] = 1 and dp[1][0][1] = 1
    For all other i and j,
        dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1]
        dp[i][j][1] = dp[i-1][j][0] + dp[i-1][j-1][1]

Then, output dp[n][k][0] + dp[n][k][1]

C++




// C++ program to count number of binary strings
// with k times appearing consecutive 1's.
#include <bits/stdc++.h>
using namespace std;
 
int countStrings(int n, int k)
{
    // dp[i][j][0] stores count of binary
    // strings of length i with j consecutive
    // 1's and ending at 0.
    // dp[i][j][1] stores count of binary
    // strings of length i with j consecutive
    // 1's and ending at 1.
    int dp[n + 1][k + 1][2];
    memset(dp, 0, sizeof(dp));
 
    // If n = 1 and k = 0.
    dp[1][0][0] = 1;
    dp[1][0][1] = 1;
 
    for (int i = 2; i <= n; i++) {
 
        // number of adjacent 1's can not exceed i-1
        for (int j = 0; j <= k; j++) {
            dp[i][j][0] = dp[i - 1][j][0] + dp[i - 1][j][1];
            dp[i][j][1] = dp[i - 1][j][0];
 
            if (j - 1 >= 0)
                dp[i][j][1] += dp[i - 1][j - 1][1];
        }
    }
 
    return dp[n][k][0] + dp[n][k][1];
}
 
// Driver code
int main()
{
    int n = 5, k = 2;
    cout << countStrings(n, k);
    return 0;
}


Java




// Java program to count number of binary strings
// with k times appearing consecutive 1's.
import java.util.*;
import java.io.*;
 
class GFG {
 
    static int countStrings(int n, int k)
    {
        // dp[i][j][0] stores count of binary
        // strings of length i with j consecutive
        // 1's and ending at 0.
        // dp[i][j][1] stores count of binary
        // strings of length i with j consecutive
        // 1's and ending at 1.
        int dp[][][] = new int[n + 1][k + 1][2];
 
        // If n = 1 and k = 0.
        dp[1][0][0] = 1;
        dp[1][0][1] = 1;
 
        for (int i = 2; i <= n; i++) {
 
            // number of adjacent 1's can not exceed i-1
            for (int j = 0; j < i && j < k + 1; j++) {
                dp[i][j][0] = dp[i - 1][j][0] + dp[i - 1][j][1];
                dp[i][j][1] = dp[i - 1][j][0];
 
                if (j - 1 >= 0) {
                    dp[i][j][1] += dp[i - 1][j - 1][1];
                }
            }
        }
 
        return dp[n][k][0] + dp[n][k][1];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 5, k = 2;
        System.out.println(countStrings(n, k));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3




# Python3 program to count number of
# binary strings with k times appearing
# consecutive 1's.
def countStrings(n, k):
 
    # dp[i][j][0] stores count of binary
    # strings of length i with j consecutive
    # 1's and ending at 0.
    # dp[i][j][1] stores count of binary
    # strings of length i with j consecutive
    # 1's and ending at 1.
    dp = [[[0, 0] for __ in range(k + 1)]
                  for _ in range(n + 1)]
 
    # If n = 1 and k = 0.
    dp[1][0][0] = 1
    dp[1][0][1] = 1
 
    for i in range(2, n + 1):
         
        # number of adjacent 1's can not exceed i-1
        for j in range(k + 1):
            dp[i][j][0] = (dp[i - 1][j][0] +
                           dp[i - 1][j][1])
            dp[i][j][1] = dp[i - 1][j][0]
            if j >= 1:
                dp[i][j][1] += dp[i - 1][j - 1][1]
 
    return dp[n][k][0] + dp[n][k][1]
 
# Driver Code
if __name__ == '__main__':
    n = 5
    k = 2
    print(countStrings(n, k))
 
# This code is contributed by vibhu4agarwal


C#




// C# program to count number of binary strings
// with k times appearing consecutive 1's.
using System;
 
class GFG {
 
    static int countStrings(int n, int k)
    {
        // dp[i][j][0] stores count of binary
        // strings of length i with j consecutive
        // 1's and ending at 0.
        // dp[i][j][1] stores count of binary
        // strings of length i with j consecutive
        // 1's and ending at 1.
        int[,, ] dp = new int[n + 1, k + 1, 2];
 
        // If n = 1 and k = 0.
        dp[1, 0, 0] = 1;
        dp[1, 0, 1] = 1;
 
        for (int i = 2; i <= n; i++) {
 
            // number of adjacent 1's can not exceed i-1
            for (int j = 0; j < i && j < k + 1; j++) {
                dp[i, j, 0] = dp[i - 1, j, 0] + dp[i - 1, j, 1];
                dp[i, j, 1] = dp[i - 1, j, 0];
 
                if (j - 1 >= 0) {
                    dp[i, j, 1] += dp[i - 1, j - 1, 1];
                }
            }
        }
 
        return dp[n, k, 0] + dp[n, k, 1];
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5, k = 2;
        Console.WriteLine(countStrings(n, k));
    }
}
 
// This code contributed by Rajput-Ji


PHP




<?php
// PHP program to count number of binary strings
// with k times appearing consecutive 1's.
 
function countStrings($n, $k)
{
    // dp[i][j][0] stores count of binary
    // strings of length i with j consecutive
    // 1's and ending at 0.
    // dp[i][j][1] stores count of binary
    // strings of length i with j consecutive
    // 1's and ending at 1.
    $dp=array_fill(0, $n + 1, array_fill(0, $k + 1, array_fill(0, 2, 0)));
 
    // If n = 1 and k = 0.
    $dp[1][0][0] = 1;
    $dp[1][0][1] = 1;
 
    for ($i = 2; $i <= $n; $i++)
    {
        // number of adjacent 1's can not exceed i-1
        for ($j = 0; $j < $i; $j++)
        {
            if(isset($dp[$i][$j][0])||isset($dp[$i][$j][1])){
              $dp[$i][$j][0] = $dp[$i - 1][$j][0] + $dp[$i - 1][$j][1];
              $dp[$i][$j][1] = $dp[$i - 1][$j][0];
            }
            if ($j - 1 >= 0 && isset($dp[$i][$j][1]))
                $dp[$i][$j][1] += $dp[$i - 1][$j - 1][1];
        }
    }
 
    return $dp[$n][$k][0] + $dp[$n][$k][1];
}
 
// Driver code
$n=5;
$k=2;
echo countStrings($n, $k);
     
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript program to count number
// of binary strings with k times
// appearing consecutive 1's.
function countStrings(n, k)
{
     
    // dp[i][j][0] stores count of binary
    // strings of length i with j consecutive
    // 1's and ending at 0.
    // dp[i][j][1] stores count of binary
    // strings of length i with j consecutive
    // 1's and ending at 1.
    let dp = new Array(n + 1);
    for(let i = 0; i < n + 1; i++)
    {
        dp[i] = new Array(k + 1);
        for(let j = 0; j < k + 1; j++)
        {
            dp[i][j] = new Array(2);
            for(let l = 0 ; l < 2; l++)
            {
                dp[i][j][l] = 0;
            }
        }
    }
 
    // If n = 1 and k = 0.
    dp[1][0][0] = 1;
    dp[1][0][1] = 1;
 
    for(let i = 2; i <= n; i++)
    {
         
        // Number of adjacent 1's can not exceed i-1
        for(let j = 0; j < i && j < k + 1; j++)
        {
            dp[i][j][0] = dp[i - 1][j][0] +
                          dp[i - 1][j][1];
            dp[i][j][1] = dp[i - 1][j][0];
 
            if (j - 1 >= 0)
            {
                dp[i][j][1] += dp[i - 1][j - 1][1];
            }
        }
    }
    return dp[n][k][0] + dp[n][k][1];
}
 
// Driver code
let n = 5, k = 2;
 
document.write(countStrings(n, k));
 
// This code is contributed by rag2127
 
</script>


Output: 

6

Time Complexity : O(n2)
Auxiliary Space: O(n2)

 



Similar Reads

Count of pairs {X, Y} from an array such that sum of count of set bits in X ⊕ Y and twice the count of set bits in X &amp; Y is M
Given an array arr[] consisting of N non-negative integers and an integer M, the task is to find the count of unordered pairs {X, Y} of array elements that satisfies the condition setBits(X ⊕ Y) + 2 * setBits(X &amp; Y) = M, where ⊕ denotes the Bitwise XOR and &amp; denotes the Bitwise AND. Examples: Input: arr[] = {30, 0, 4, 5 }, M = 2Output: 2Exp
14 min read
Count of numbers appearing in the given ranges at-least K times
Given N ranges and a number K, the task is to find the total count of numbers that appear at least K times in the given ranges. Examples: Input: N = 3, K = 2 Range 1: [91, 94] Range 2: [92, 97] Range 3: [97, 99] Output : 4Explanation: Ranges are 91 to 94, 92 to 97, 97 to 99 and the numbers that occurred atleast 2 times are 92, 93, 94, 97. Input : N
12 min read
Generate all binary strings of length n with sub-string "01" appearing exactly twice
Given an integer N, the task is to generate all possible binary strings of length N which contain "01" as the sub-string exactly twice. Examples: Input: N = 4 Output: 0101 "0101" is the only binary string of length 4 that contains "01" exactly twice as the sub-string. Input: N = 5 Output: 00101 01001 01010 01011 01101 10101 Recommended: Please try
16 min read
Count strings from given array having all characters appearing in a given string
Given an array of strings arr[][] of size N and a string S, the task is to find the number of strings from the array having all its characters appearing in the string S. Examples: Input: arr[][] = {"ab", "aab", "abaaaa", "bbd"}, S = "ab"Output: 3Explanation: String "ab" have all the characters occurring in string S.String "aab" have all the charact
6 min read
Number of Binary Strings of length N with K adjacent Set Bits
Given [Tex]n [/Tex]and [Tex]k [/Tex]. The task is to find the number of binary strings of length n out of 2n such that they satisfy f(bit string) = k. Where, f(x) = Number of times a set bit is adjacent to another set bit in a binary string x.For Example:f(011101101) = 3f(010100000) = 0f(111111111) = 8Examples: Input : n = 5, k = 2Output : 6Explana
23 min read
Find the elements appearing even number of times in an Array
Given an array arr[] consisting of N positive numbers in the range [1, N], the task is to print the array elements which appear even number of times in the given array. Example : Input: N = 8, arr[] = {4, 4, 2, 4, 8, 2, 3, 4}Output: [2, 4]Explanation: The numbers 2 and 4 appear even number of times in the array. Input: N=4, arr[] = {3, 1, 4, 4}Outp
10 min read
Length of the longest substring with every character appearing even number of times
Content has been removed on Author’s request.
1 min read
Print all array elements appearing more than N / K times
Given an array arr[] of size N and an integer K, the task is to find all the array elements that appear more than (N / K) times. Examples: Input: arr[] = { 1, 2, 6, 6, 6, 6, 6, 10 }, K = 4Output: 6Explanation: The frequency of 6 in the array is greater than N / K(= 2). Therefore, the required output is 6. Input: arr[] = { 3, 4, 4, 5, 5, 5, 5 }, K =
21 min read
Flip bits of the sum of count of set bits of two given numbers
Given two numbers A and B, the task is to count the number of set bits in A and B and flip the bits of the obtained sum. Examples: Input: A = 5, B = 7Output: 2Explanation:Binary representation of A is 101.Binary representation of B is 111.Count of set bits in A and B = 2 + 3 = 5.Binary representation of the sum obtained = 101Flipping the bits of th
5 min read
Minimum number of flipping adjacent bits required to make given Binary Strings equal
Given two binary strings s1[] and s2[] of the same length N, the task is to find the minimum number of operations to make them equal. Print -1 if it is impossible to do so. One operation is defined as choosing two adjacent indices of one of the binary string and inverting the characters at those positions, i.e, 1 to 0 and vice-versa. Examples: Inpu
7 min read
Practice Tags :