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
0inarris exactlyzero. - The number of occurrences of
1inarris exactlyone. - Each subarray of
arrwith a size greater thanlimitmust contain both0and1.
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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code