Open In App

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

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:

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

Below is the implementation of the above approach.




// 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 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.




# 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




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.




<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)

 


Article Tags :