Skip to main content
Back to problems
Leetcode
Medium
Arrays
Dynamic Programming
Combinatorics
Google
Find All Possible Stable Binary Arrays II

Count the number of binary arrays that satisfy run-length stability constraints.

Acceptance 0%
Problem Statement

You are given counts of zeros and ones, along with a limit on how long any consecutive block of equal values may be. Count how many binary arrays can be formed using exactly the given number of 0s and 1s such that every run of identical bits has length at most the allowed limit.

Return the answer modulo 109+710^9 + 7.

This is a counting problem: instead of constructing the arrays, determine how many valid arrangements exist.

Input Format

A typical input consists of three integers:

  • zero: the number of 0s to use
  • one: the number of 1s to use
  • limit: the maximum allowed length of any consecutive run of equal bits

Output Format

Return a single integer: the number of valid binary arrays modulo 109+710^9 + 7.

Constraints

  • 0 <= zero, one
  • 1 <= limit
  • The answer may be large, so modulo arithmetic is required.
  • The exact platform constraints are not provided here; the intended solution should handle large counts efficiently with dynamic programming.
Examples
Sample cases returned by the problem API.

Example 1

Input

zero = 2, one = 2, limit = 1

Output

2

Explanation

Only alternating arrays are valid: [0,1,0,1] and [1,0,1,0].

Example 2

Input

zero = 3, one = 1, limit = 2

Output

1

Explanation

The only valid arrangement is [0,0,1,0]. Arrays with three consecutive 0s are not allowed.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.