Open In App

Count of Binary strings having at most X consecutive 1s and Y consecutive 0s

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers N and M (1 ≤ N, M ≤ 100) denoting the total number of 1s and 0s respectively. The task is to count the number of possible arrangements of these 0s and 1s such that any arrangement has at most X consecutive 1s and Y consecutive 0s (1 ≤ X, Y ≤ 10). As the number of arrangements can be very large, compute the answer with MODULO 109+7.

Examples:

 Input: N = 2, M = 3, X = 1, Y = 2
Output: 5
Explanation: All arrangements: 11000, 10100, 10010, 10001, 01100, 01010, 01001, 00110, 00101, 00011.
Out of these arrangements the valid arrangements are: 10100, 10010, 01010, 01001, 00101
So the number of arrangements possible are 5.

Input: N = 2, M = 2, X = 1, Y = 1
Output: 2

 

Intuition:

  • The basic intuition of the problem is to check all possible arrangements using recursion.
  • This leads to extremely high time complexity.
  • So need of some optimization technique.
  • Opting tabulation. This reduces the overall complexity of the problem by drastic factor.
  • Using iterative approach over the traditional recursion + memoization makes it even simpler.

Approach: Based on the above intuition this problem can be solved using dynamic programming approach. Follow the steps mentioned below to solve the problem.

  • Use a 3D grid for storing the following {count of 1s, count of 0s, represents the last value which was used (0 if A,1 if B)}
  • Use nested loop for 1s and 0s.
  • At each iteration in nested loop,(i  denotes 1s, j denotes 0s)
    • If 1s are added to the current sequence, Add the number of sequences formed with i – k, (1 ≤ k ≤ X) 1s and ends with 0s.
    • If 0s are added to the current sequence, Add the number of sequences formed with j – k, (1 ≤ k ≤ Y) 0s and ends with 1s.
  • This gives the answer to each iteration of sequence with i 1s and j 0s.

Below is the implementation of the above approach.

C++




// C++ code to implement above approach
#include <bits/stdc++.h>
using namespace std;
 
// Functiont to calculate
// total possible arrangements
int totalArrangements(int N, int M,
                      int X, int Y)
{
    int dp[N + 1][M + 1][2];
    int mod = 1000000007;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            dp[i][j][0] = 0;
            dp[i][j][1] = 0;
        }
    }
    dp[0][0][0] = 1;
    dp[0][0][1] = 1;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            for (int k = 1; k <= X; k++) {
                if (i >= k) {
                    dp[i][j][1]
                        += dp[i - k][j][0];
                    dp[i][j][1] %= mod;
                }
            }
            for (int k = 1; k <= Y; k++) {
                if (j >= k) {
                    dp[i][j][0]
                        += dp[i][j - k][1];
                    dp[i][j][0] %= mod;
                }
            }
        }
    }
    return ((dp[N][M][0] + dp[N][M][1]) % mod);
}
 
// Driver code
int main()
{
    int N = 2, M = 3, X = 1, Y = 2;
     
    cout << totalArrangements(N, M, X, Y);
    return 0;
}


Java




// Java code for the above approach
import java.util.*;
 
class GFG{
 
// Functiont to calculate
// total possible arrangements
static int totalArrangements(int N, int M,
                      int X, int Y)
{
    int dp[][][] = new int[N + 1][M + 1][2];
    int mod = 1000000007;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            dp[i][j][0] = 0;
            dp[i][j][1] = 0;
        }
    }
    dp[0][0][0] = 1;
    dp[0][0][1] = 1;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= M; j++) {
            for (int k = 1; k <= X; k++) {
                if (i >= k) {
                    dp[i][j][1]
                        += dp[i - k][j][0];
                    dp[i][j][1] %= mod;
                }
            }
            for (int k = 1; k <= Y; k++) {
                if (j >= k) {
                    dp[i][j][0]
                        += dp[i][j - k][1];
                    dp[i][j][0] %= mod;
                }
            }
        }
    }
    return ((dp[N][M][0] + dp[N][M][1]) % mod);
}
 
// Driver Code
public static void main(String[] args)
{
     int N = 2, M = 3, X = 1, Y = 2;
     
    System.out.print(totalArrangements(N, M, X, Y));
}
}
 
// This code is contributed by sanjoy_62.


Python3




# Python code to implement above approach
 
# Functiont to calculate
# total possible arrangements
def totalArrangements(N, M, X, Y):
    dp = [[[0 for i in range(2)] for j in range(M + 1) ] for k in range(N + 1)]
    mod = 1000000007;
    for i in range(N + 1):
        for j in range(M + 1):
            dp[i][j][0] = 0;
            dp[i][j][1] = 0;
    dp[0][0][0] = 1;
    dp[0][0][1] = 1;
    for i in range(N + 1):
        for j in range(M + 1):
            for k in range(1, X + 1):
                if (i >= k):
                    dp[i][j][1] += dp[i - k][j][0];
                    dp[i][j][1] %= mod;
            for k in range(1, Y + 1):
                if (j >= k):
                    dp[i][j][0] += dp[i][j - k][1];
                    dp[i][j][0] %= mod;
    return ((dp[N][M][0] + dp[N][M][1]) % mod);
 
# Driver code
N = 2
M = 3
X = 1
Y = 2;
print(totalArrangements(N, M, X, Y));
 
# This code is contributed by gfgking


C#




using System;
 
public class GFG{
 
  // Functiont to calculate
  // total possible arrangements
  static int totalArrangements(int N, int M,
                               int X, int Y)
  {
    int[,,] dp = new int[N + 1, M + 1, 2];
    int mod = 1000000007;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= M; j++) {
        dp[i, j, 0] = 0;
        dp[i, j, 1] = 0;
      }
    }
    dp[0, 0, 0] = 1;
    dp[0, 0, 1] = 1;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= M; j++) {
        for (int k = 1; k <= X; k++) {
          if (i >= k) {
            dp[i, j, 1]
              += dp[i - k, j, 0];
            dp[i, j, 1] %= mod;
          }
        }
        for (int k = 1; k <= Y; k++) {
          if (j >= k) {
            dp[i, j, 0]
              += dp[i, j-k, 1];
            dp[i, j, 0] %= mod;
          }
        }
      }
    }
    return ((dp[N, M, 0] + dp[N, M,1]) % mod);
  }
 
  // Driver code
  static public void Main ()
  {
 
    int N = 2, M = 3, X = 1, Y = 2;
    Console.WriteLine(totalArrangements(N, M, X, Y));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
    // JavaScript code to implement above approach
 
    // Functiont to calculate
    // total possible arrangements
    const totalArrangements = (N, M, X, Y) => {
        let dp = new Array(N + 1).fill(0).map(() => new Array(M + 1).fill(0).map(() => new Array(2)));
        let mod = 1000000007;
        for (let i = 0; i <= N; i++) {
            for (let j = 0; j <= M; j++) {
                dp[i][j][0] = 0;
                dp[i][j][1] = 0;
            }
        }
        dp[0][0][0] = 1;
        dp[0][0][1] = 1;
        for (let i = 0; i <= N; i++) {
            for (let j = 0; j <= M; j++) {
                for (let k = 1; k <= X; k++) {
                    if (i >= k) {
                        dp[i][j][1]
                            += dp[i - k][j][0];
                        dp[i][j][1] %= mod;
                    }
                }
                for (let k = 1; k <= Y; k++) {
                    if (j >= k) {
                        dp[i][j][0]
                            += dp[i][j - k][1];
                        dp[i][j][0] %= mod;
                    }
                }
            }
        }
        return ((dp[N][M][0] + dp[N][M][1]) % mod);
    }
 
    // Driver code
    let N = 2, M = 3, X = 1, Y = 2;
    document.write(totalArrangements(N, M, X, Y));
 
// This code is contributed by rakeshsahni
 
</script>


 
 

Output

5

 

Time Complexity: O(N * M * (X+Y)).
Auxiliary Space: O(N * M)

 



Last Updated : 01 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads