How would you like to learn today? Visualize algorithms in real time, explore them step by step through reading, or challenge yourself with a test.
Visualize the algorithm step-by-step with interactive animations. See exactly how the code executes in real time.
Read comprehensive problem explanation, reference links, and explore the code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
Difficulty: š” Medium
Topics: Dynamic Programming, Combinatorics
You are given three positive integers:
zero ā Number of 0'sone ā Number of 1'slimit ā Maximum allowed consecutive identical elementsA binary array arr is called stable if:
0 in arr is exactly zero1 in arr is exactly onelimit must contain both 0 and 1Return the total number of stable binary arrays.
Since the answer may be very large, return it modulo:
10^9 + 7
Input:
zero = 1, one = 1, limit = 2
Output:
2
Explanation:
The two possible stable binary arrays are:
[1,0][0,1]Both arrays:
0 and one 1Input:
zero = 1, one = 2, limit = 1
Output:
1
Explanation:
The only stable binary array is:
[1,0,1]The arrays:
[1,1,0][0,1,1]ā Are NOT stable because they contain subarrays of length 2 with identical elements.
Input:
zero = 3, one = 3, limit = 2
Output:
14
Explanation:
All possible stable arrays:
[0,0,1,0,1,1]
[0,0,1,1,0,1]
[0,1,0,0,1,1]
[0,1,0,1,0,1]
[0,1,0,1,1,0]
[0,1,1,0,0,1]
[0,1,1,0,1,0]
[1,0,0,1,0,1]
[1,0,0,1,1,0]
[1,0,1,0,0,1]
[1,0,1,0,1,0]
[1,0,1,1,0,0]
[1,1,0,0,1,0]
[1,1,0,1,0,0]
1 <= zero, one, limit <= 200
The constraint:
Any subarray of length > limit must contain both 0 and 1
Means:
š You cannot have more than limit consecutive 0's
š You cannot have more than limit consecutive 1's
This is a Dynamic Programming + Counting problem.
import java.util.Arrays;
public class Main {
static int MOD = 1_000_000_007;
static int[][][][] memo;
static int limit;
public static void main(String[] args) {
// š¹ Hardcoded Inputs
int zero = 3;
int one = 3;
limit = 2;
memo = new int[zero + 1][one + 1][2][limit + 1];
for (int[][][] a : memo)
for (int[][] b : a)
for (int[] c : b)
Arrays.fill(c, -1);
int result = 0;
// Start with 0
if (zero > 0)
result = (result + dfs(zero - 1, one, 0, 1)) % MOD;
// Start with 1
if (one > 0)
result = (result + dfs(zero, one - 1, 1, 1)) % MOD;
System.out.println("Total Stable Arrays: " + result);
}
static int dfs(int zeroLeft, int oneLeft, int last, int count) {
if (zeroLeft == 0 && oneLeft == 0)
return 1;
if (memo[zeroLeft][oneLeft][last][count] != -1)
return memo[zeroLeft][oneLeft][last][count];
long ways = 0;
// Try placing 0
if (zeroLeft > 0) {
if (last == 0) {
if (count < limit)
ways += dfs(zeroLeft - 1, oneLeft, 0, count + 1);
} else {
ways += dfs(zeroLeft - 1, oneLeft, 0, 1);
}
}
// Try placing 1
if (oneLeft > 0) {
if (last == 1) {
if (count < limit)
ways += dfs(zeroLeft, oneLeft - 1, 1, count + 1);
} else {
ways += dfs(zeroLeft, oneLeft - 1, 1, 1);
}
}
return memo[zeroLeft][oneLeft][last][count] = (int)(ways % MOD);
}
}
⨠Ready to visualize or solve it?
Use your DSA canvas and try building the transitions!
Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
š Locked steps cannot be moved. Only unlocked steps are draggable.
š Enable sound for swap feedback and completion effects.
Ā© 2026 | Privacy Policy | About