Find All Possible Stable Binary Arrays I - Problem

You are tasked with counting the number of stable binary arrays that can be constructed given specific constraints.

Given three positive integers zero, one, and limit, you need to find how many different binary arrays satisfy these conditions:

  • The array contains exactly zero occurrences of digit 0
  • The array contains exactly one occurrences of digit 1
  • No subarray longer than limit can consist entirely of the same digit (ensuring balance)

Example: If zero=1, one=1, limit=2, valid arrays are [0,1] and [1,0] โ†’ answer is 2

Return the count modulo 109 + 7 since the result can be extremely large.

Input & Output

example_1.py โ€” 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 consecutive runs exceed limit=2.
example_2.py โ€” Multiple Options
$ Input: zero = 1, one = 3, limit = 1
โ€บ Output: 0
๐Ÿ’ก Note: Need exactly 1 zero and 3 ones, but limit=1 means no consecutive identical elements. Impossible to place 3 ones without violating the constraint.
example_3.py โ€” Higher Limit
$ Input: zero = 2, one = 2, limit = 3
โ€บ Output: 6
๐Ÿ’ก Note: Valid arrangements: [0,0,1,1], [0,1,0,1], [0,1,1,0], [1,0,0,1], [1,0,1,0], [1,1,0,0]. All satisfy the consecutive constraint with limit=3.

Visualization

Tap to expand
Building Stable Arrays (zero=2, one=2, limit=2)Step 1: InitializeNeed: 2 zeros, 2 onesArray: [ ]Step 2: First ChoicePlace 0 or 1?Options: [0,...] [1,...]Step 3: Track StateLast: 0, Consecutive: 1Remaining: 1 zero, 2 onesStep 4: ContinueCheck limit constraintMake next choiceDecision Tree ExampleStart[0][1]place 0place 1[0,0][0,1][1,0][1,1]Key InsightInstead of generating all arrays, we count valid paths through the decision treeusing dynamic programming to avoid redundant computations!
Understanding the Visualization
1
Start Fresh
Begin with empty array, knowing we need exactly 'zero' 0s and 'one' 1s
2
Make Choices
At each position, decide whether to place 0 or 1 based on remaining quota
3
Check Constraints
Ensure we don't exceed 'limit' consecutive identical elements
4
Count Valid Paths
Sum all possible ways to complete the array satisfying all conditions
Key Takeaway
๐ŸŽฏ Key Insight: By tracking state (remaining elements, last placed, consecutive count) we can count valid arrangements without explicitly generating them, achieving optimal O(zero ร— one ร— limit) complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(zero ร— one ร— limit)

Each unique state (zeros_left, ones_left, last_element, consecutive) is computed once

n
2n
โœ“ Linear Growth
Space Complexity
O(zero ร— one ร— limit)

Memoization table stores results for all possible states

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค zero, one โ‰ค 1000
  • 1 โ‰ค limit โ‰ค zero + one
  • The answer should be returned modulo 109 + 7
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
23.5K Views
Medium Frequency
~25 min Avg. Time
847 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