Find All Possible Stable Binary Arrays I โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโ€”or scroll down to preview all options.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

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

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

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!

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

Categories
dp
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/find-all-possible-stable-binary-arrays-i/description/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

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.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.