Find All Possible Stable Binary Arrays I - Problem

You are given 3 positive integers zero, one, and limit.

A binary array arr is called stable if:

  • The number of occurrences of 0 in arr is exactly zero.
  • The number of occurrences of 1 in arr is exactly one.
  • Each subarray of arr with a size greater than limit must contain both 0 and 1.

Return the total number of stable binary arrays. Since the answer may be very large, return it modulo 109 + 7.

Input & Output

Example 1 — Basic Case
$ Input: zero = 1, one = 1, limit = 2
Output: 2
💡 Note: Two valid arrays: [0,1] and [1,0]. Both have exactly 1 zero and 1 one, and no subarray of length > 2 exists.
Example 2 — Larger Limit
$ Input: zero = 1, one = 2, limit = 1
Output: 1
💡 Note: Only [1,0,1] is valid. Arrays like [1,1,0] would have two consecutive 1s, violating limit=1.
Example 3 — Higher Counts
$ Input: zero = 3, one = 1, limit = 2
Output: 3
💡 Note: Valid arrays are [0,0,1,0], [0,1,0,0], and [1,0,0,0]. No more than 2 consecutive same digits allowed.

Constraints

  • 1 ≤ zero, one, limit ≤ 200

Visualization

Tap to expand
Stable Binary Arrays: zero=1, one=1, limit=2Requirements:1 zero, 1 one, limit=2Constraint:≤ 2 consecutive same[0,1][1,0]InvalidValid ✓Valid ✓Wrong countsBoth arrays satisfy: exact counts + no violationsResult: 2 stable arrays
Understanding the Visualization
1
Input
Need 1 zero, 1 one, limit=2 consecutive
2
Build
Generate valid arrangements: [0,1] and [1,0]
3
Count
Return total number of stable arrays: 2
Key Takeaway
🎯 Key Insight: Use dynamic programming to track state transitions while respecting consecutive digit limits
Asked in
Google 25 Amazon 18 Facebook 15
12.0K Views
Medium Frequency
~35 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen