Find All Possible Stable Binary Arrays II - Problem

You are tasked with constructing stable binary arrays - a fascinating combinatorial problem that tests your dynamic programming skills!

Given three positive integers zero, one, and limit, you need to count how many different binary arrays can be formed that satisfy these conditions:

  • The array contains exactly zero occurrences of 0
  • The array contains exactly one occurrences of 1
  • No subarray of length greater than limit can contain only 0s or only 1s (this ensures stability)

The third condition is the key constraint - it prevents long streaks of consecutive identical digits, making the array "stable" by ensuring diversity in longer subsequences.

Return the total number of such stable binary arrays. Since this number can be astronomically large, return it modulo 109 + 7.

Example: If zero=1, one=1, limit=2, valid arrays are [0,1] and [1,0], so the answer is 2.

Input & Output

example_1.py — Basic Case
$ Input: zero = 1, one = 1, limit = 2
Output: 2
💡 Note: With 1 zero and 1 one, we can form arrays [0,1] and [1,0]. Both are stable since no subarray longer than limit=2 contains only one type of digit. Total: 2 valid arrays.
example_2.py — Longer Array
$ Input: zero = 1, one = 2, limit = 1
Output: 1
💡 Note: With limit=1, no two consecutive digits can be the same. The only valid arrangement is [0,1,0] - we must alternate. Arrays like [1,1,0] are invalid because '11' exceeds the limit.
example_3.py — Higher Limit
$ Input: zero = 3, one = 3, limit = 2
Output: 14
💡 Note: With limit=2, we can have at most 2 consecutive 0s or 1s. This allows many more arrangements than alternating, but still prevents long streaks like '000' or '111'.

Constraints

  • 1 ≤ zero, one ≤ 200
  • 1 ≤ limit ≤ 200
  • Answer must be returned modulo 109 + 7
  • Array length will be zero + one

Visualization

Tap to expand
Stable Binary Arrays II INPUT Binary Array Structure ? ? Array length = zero + one = 2 Parameters: zero = 1 one = 1 limit = 2 No subarray longer than limit with same digits MOD = 10^9 + 7 ALGORITHM STEPS 1 Define DP State dp[i][j][k] = ways with i 0s, j 1s, ending with k 2 Track Consecutive Count streak length to enforce limit constraint 3 Apply Transitions Add 0 or 1 if streak stays within limit 4 Sum Valid States Count all dp[zero][one][*] with valid endings Valid Arrays: 0 1 [0,1] 1 0 [1,0] FINAL RESULT Stable Binary Arrays Found: Array 1: 0 1 OK Array 2: 1 0 OK OUTPUT 2 Both arrays are stable: No streak exceeds limit=2 Result: 2 mod (10^9+7) = 2 Key Insight: The DP state must track not just counts of 0s and 1s, but also the last digit placed to handle the streak constraint. With limit=2, any arrangement works for arrays of length 2, but longer arrays need careful counting. Use inclusion-exclusion or optimized DP to avoid TLE on large inputs. TutorialsPoint - Find All Possible Stable Binary Arrays II | Optimal DP Solution
Asked in
Google 28 Amazon 15 Meta 12 Microsoft 8
26.5K Views
Medium Frequency
~35 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