3129. Find All Possible Stable Binary Arrays I
Difficulty: Medium
Topics: Staff, Dynamic Programming, Prefix Sum, Biweekly Contest 129
You are given 3 positive integers zero, one, and limit.
A binary array1 arr is called stable if:
- The number of occurrences of 0 in
arris exactlyzero. - The number of occurrences of 1 in
arris exactlyone. - Each subarray2 of
arrwith a size greater thanlimitmust contain both 0 and 1.
Return the total number of stable binary arrays.
Since the answer may be very large, return it modulo 10⁹ + 7.
Example 1:
- Input: zero = 1, one = 1, limit = 2
- Output: 2
-
Explanation: The two possible stable binary arrays are
[1,0]and[0,1], as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2.
Example 2:
- Input: zero = 1, one = 2, limit = 1
- Output: 1
-
Explanation:
- The only possible stable binary array is
[1,0,1]. - Note that the binary arrays
[1,1,0]and[0,1,1]have subarrays of length 2 with identical elements, hence, they are not stable.
- The only possible stable binary array is
Example 3:
- Input: zero = 3, one = 3, limit = 2
- Output: 14
-
Explanation: All the possible stable binary arrays are
[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], and[1,1,0,1,0,0].
Constraints:
1 <= zero, one, limit <= 200
Hint:
- Let
dp[a][b][c = 0/1][d]be the number of stable arrays with exactlya0s,b1s and consecutivedvalue ofc’s at the end. - Try each case by appending a 0/1 at last to get the inductions.
Solution:
The problem asks for the number of binary arrays containing exactly zero zeros and one ones such that any contiguous subarray longer than limit contains both 0 and 1. This condition is equivalent to prohibiting any run of consecutive identical bits longer than limit. The solution uses a combinatorial run‑based approach: count the ways to split the zeros into a certain number of runs (each of length between 1 and limit) and similarly for the ones, then multiply the possibilities for runs that can interlace correctly.
Approach:
- Reduce the problem to counting sequences where no run (block of equal bits) exceeds
limit. - Let
r0be the number of runs of zeros andr1the number of runs of ones. Because runs alternate,|r0 – r1| ≤ 1. - For a given number of runs
k, computef0[k]= number of ways to partitionzerozeros intokruns, each run length in[1, limit]. Similarlyf1[l]foroneones.- Use inclusion‑exclusion on the classic stars‑and‑bars formula:
f[k] = Σ_{j=0}^{⌊(n−k)/limit⌋} (−1)^j * C(k, j) * C(n − j·limit − 1, k − 1).
- Use inclusion‑exclusion on the classic stars‑and‑bars formula:
- Sum over all valid
(r0, r1)pairs, distinguishing arrays that start with 0 or with 1:- Starting with 0: either
r0 = r1(ends with 1) orr0 = r1 + 1(ends with 0). - Starting with 1: either
r1 = r0(ends with 0) orr1 = r0 + 1(ends with 1).
- Starting with 0: either
- All computations are performed modulo
10⁹ + 7.
Let's implement this solution in PHP: 3129. Find All Possible Stable Binary Arrays I
<?php
/**
* @param Integer $zero
* @param Integer $one
* @param Integer $limit
* @return Integer
*/
function numberOfStableArrays(int $zero, int $one, int $limit): int
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* Fast modular exponentiation
*
* @param $a
* @param $b
* @param $mod
* @return int
*/
function powmod($a, $b, $mod): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo numberOfStableArrays(1, 1, 2) . "\n"; // Output: 2
echo numberOfStableArrays(1, 2, 1) . "\n"; // Output: 1
echo numberOfStableArrays(3, 3, 2) . "\n"; // Output: 14
?>
Explanation:
-
Condition reinterpretation – A subarray longer than
limitcontaining only zeros or only ones would violate the rule; hence every run of zeros or ones can be at mostlimitlong. Conversely, if all runs are ≤limit, any subarray longer thanlimitmust cross a run boundary and therefore contain both bits. -
Run decomposition – Any binary array is uniquely described by its sequence of runs. For a fixed starting bit, the numbers of runs
r0andr1determine the pattern. -
Counting runs of a given length – Distributing
nidentical items intoknon‑empty blocks with an upper boundLon each block is a constrained composition problem. The inclusion‑exclusion formula given above counts exactly those compositions. -
Combining runs – Once
r0andr1are fixed, the zeros runs and ones runs can be interleaved in exactly one way (the pattern is forced by the starting bit). Therefore the total number of arrays for that pair isf0[r0] * f1[r1]. -
Summation – The loops iterate over all feasible
r0, r1(from 1 up to the available counts) and add the products for both possible starting bits, avoiding double counting because arrays with a given starting bit are disjoint from those with the opposite starting bit.
Complexity
-
Time Complexity: Precomputing factorials and inverse factorials up to
maxN = 400costsO(maxN). Computing eachf0[k]andf1[l]runs a loop overjup to roughlyn/L, so total work isO(zero·(zero/limit) + one·(one/limit)). Withzero, one ≤ 200, this is easily within limits. -
Space Complexity:
O(zero + one + maxN)for the factorial arrays and thef0, f1tables.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)