Breaking a set into continuous subsets such that number of $1$’s are greater than number of $0$’s

combinatoricscomputer sciencepermutations

You are given a list of $0$’s and $1$’s: $B[1], B[2],\ldots, B[N]$. A
sublist of this list is any contiguous segment of elements—i.e.,
$A[i], A[i + 1],\ldots , A[j]$, for some $i$ and $j$.

A sublist is said to be Heavy, if the number of $1$’s in it is at
least as much as the number of $0$’s in it.

We want to partition the entire list into Heavy sublists. That is, a
valid partition is a collection of Heavy sublists, such that each of
the $N$ elements is part of exactly one of the sublists. We want to
find the number of ways of doing so.

For example, suppose $N$ was $3$ and $B = [1, 0, 1]$. Then all the
sublists in this are Heavy, except for the sublist which contains only
the second element $([0])$. The various valid partitions are as
follows:

  • $( [1, 0, 1] )$
  • $( [1, 0], [1] )$
  • $( [1], [0, 1] )$

Since there are $3$ ways to do this, the answer for this would be $3$.

Compute the number of ways of partitioning the given list into Heavy
sublists for the following instances.

(a) $N = 8, B = [0, 1, 1, 0, 0, 1, 1, 1]—i.e., B[1] = 0, B[2] =
> 1,\ldots , B[8] = 1$

(b) $N = 9, B = [1, 1, 0, 0, 1, 0, 0, 1, 1]—i.e., B[1] = 1, B[2] =
> 1,\ldots , B[9] = 1$

(c) $N = 9, B = [1, 0, 1, 0, 1, 1, 0, 1, 1]—i.e., B[1] = 1, B[2] =
> 0,\ldots , B[9] = 1$

My Attempt:

I tried considering each $0$ to be $-1$ and tried calculating all the subsets that would give me a sum $\geq 0$. But we need to make sure all the partitions follow this rule. This makes my method too long and difficult to compute.

Can anyone give an idea for a better method.

Best Answer

I think you want to use a recursive algorithm. Call a sublists starting at $B[1]$ a "prefix" and a sublist ending at $B[N]$ a "suffix." For each possible way of diving the list into a suffix and a prefix, if the prefix is heavy, recursively count the number of ways of dividing the suffix into heavy sublists. (Remember to count the division into just the suffix itself.) Add up all of these, and add $1$, for the division consisting of just the original list.