Track & Visualize

Find All Possible Stable Binary Arrays I

Choose Your Learning Path

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.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations. See exactly how the code executes in real time.

šŸ“– Passive Learning

Read comprehensive problem explanation, reference links, and explore the code at your own pace.

šŸŽÆ Mandatory Challenge

Drag and arrange the algorithm steps in the correct execution order.

Understanding Find All Possible Stable Binary Arrays I
Detailed explanation and reference materials
Problem Overview

Find All Possible Stable Binary Arrays I

Difficulty: 🟔 Medium
Topics: Dynamic Programming, Combinatorics


šŸ“˜ Problem Description

You are given three positive integers:

  • zero → Number of 0's
  • one → Number of 1's
  • limit → Maximum allowed consecutive identical elements

A binary array arr is called stable if:

āœ… Conditions:

  1. The number of occurrences of 0 in arr is exactly zero
  2. The number of occurrences of 1 in arr is exactly one
  3. Every subarray with size greater than limit must contain both 0 and 1

šŸŽÆ Task

Return the total number of stable binary arrays.

Since the answer may be very large, return it modulo:

10^9 + 7

🧠 Example 1

Input:

zero = 1, one = 1, limit = 2

Output:

2

Explanation:

The two possible stable binary arrays are:

  • [1,0]
  • [0,1]

Both arrays:

  • Contain exactly one 0 and one 1
  • No subarray length exceeds 2

🧠 Example 2

Input:

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.


🧠 Example 3

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]

šŸ”’ Constraints

1 <= zero, one, limit <= 200

šŸ’” Key Insight

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.


šŸ§‘ā€šŸ’» Java Code (Hardcoded Input, No Scanner)

java
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!


Categories
dp
java
Reference Link
https://leetcode.com/problems/find-all-possible-stable-binary-arrays-i/description/
Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm , Test Me now šŸŽ®
JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.

🧠 Logic Puzzle
Arrange the Algorithm Correctly 🧩

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 Algorithm

Don't Know Current Algorithm ? Ā 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

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