FindingMath

Mixing zeros and ones unevenly between two blocks of zeros gives the best chance of reading 01010

Version 1 Published 2026-11-27 DOI 10.5281/zenodo.24586976

Importance (self-rated) Notable

This separates all bounded-run constructions from the optimum for a family of alternating words and gives the sharp density and unique limit profile for its first member.

Why it matters

Pick five positions in a string of zeros and ones and read them in order. The best long strings give a 4.919% chance of reading 01010: they start and end with zeros and mix the two letters in between, with more ones toward the edges of the mixed region. Any fixed number of solid blocks falls short, however carefully their lengths are chosen.

Choose five positions at random in a long string of zeros and ones. How should we arrange the string to give the best chance that those positions, read from left to right, spell 01010? The positions can be far apart, so alternating the letters at every step need not be best. For simpler patterns, choosing the lengths of a few solid blocks suffices. The question is whether some fixed number of blocks can do just as well as a more finely mixed string when the string grows long.

For 01010, the highest probability in the long-string limit is 4.91878025…%. An arrangement that attains this limit starts and ends with a solid block of zeros. In between, the letters mix, with a higher proportion of ones near the edges of the mixed region than at its center. Every fixed bound on the number of blocks leaves a positive gap below the best limiting probability. This last result holds for every alternating pattern of odd length at least five.

Patterns in finite strings

A copy of a pattern is a set of positions that spell it in order. Its density in a string, also called a word, is the fraction of position sets of the required size that give a copy. A run is a block of identical letters that cannot be extended in either direction. Menon and Singh [1] proved that for a pattern with at most three runs, some word of maximum density has exactly as many runs as the pattern. They also found a contrast: for an alternating pattern of length at least four, a word two letters longer does best by alternating throughout. That example leaves open how many runs are needed in much longer words.

The size of the alphabet matters for this question. Burstein, Hästö and Mansour [2] count a match if the selected letters have the same relative order as the pattern, including ties. With just two ordered letters, their 12121 is our 01010, and they denote its limiting maximum density by \delta(12121,2). Their unrestricted density \delta(12121) allows matches using other pairs of letters in a larger alphabet. Bruda [3] counts binary words with a prescribed number of copies and gives bounds on the count of copies in terms of runs. We found no previous value for the limiting maximum density of 01010 in binary words.

We counted copies of every nonempty binary pattern of length at most seven in all 16,777,214 nonempty words through length 23. At length 23, a single word gave the most copies of 01010. It had 19 runs and 2,592 copies, alternating except for three zeros at each end. Yet at length 19 the unique best word had pairs of consecutive ones in its interior. For the seven-letter pattern 0101010, every best word at length 23 had at least 21 runs. To pass from these finite counts to a limiting probability, it helps to choose the ones in a copy first, then count how many choices of zeros remain.

Counting the choices of zeros

Consider a pattern that starts with a zero followed by r repetitions of 10. We write it as 0(10)^r; its length is k=2r+1. Exchanging zeros and ones gives the other alternating pattern of that length. In a word containing Z zeros and M ones, give each one a coordinate: the fraction of all zeros that precede it. Ones in the same run share a coordinate. The distance between two coordinates is the fraction of zeros available between those two ones.

Let \mu be the distribution of the coordinate of a uniformly chosen one: each placement contributes mass 1/M, including repetitions. Draw r times independently from this distribution. Sort the draws as x_1\le\cdots\le x_r and add the endpoints x_0=0 and x_{r+1}=1. Write the mean product of the gaps as

F_r(\mu)=\mathbb E\prod_{i=0}^r(x_{i+1}-x_i).
To complete a copy we need one zero in each gap, including the two outer gaps. Multiplying each gap length by Z gives the number of choices there. Tied coordinates give a product of zero, including when the draws pick the very same one twice. There are M^r possible draws, and the ones in each successful choice can be drawn in r! orders. The exact number of copies is therefore Z^{r+1}M^r F_r(\mu)/r!. A word missing either letter has no copies.

In the limit we can choose the fraction of zeros and the placement distribution separately. To see why, first note that the gap product varies continuously with the coordinates, even when they coincide. Thus convergence in distribution of the placements implies convergence of F_r. The probability distributions on the unit interval form a compact set under this convergence, so some distribution maximizes F_r.

Conversely, we can approximate any such distribution by actual words. Give each of the M ones a coordinate by taking the quantiles at probabilities (j-1/2)/M, where j ranges from one to M. These placements converge in distribution to the desired profile. Round each coordinate to the nearest available gap in the word, that is, the nearest point of \{0,1/Z,\ldots,1\}, and insert the one there. Rounding moves it by at most 1/Z.

Write n=Z+M for the word length and p=Z/n for its zero fraction. The denominator of the density, \binom{n}{k}, divided by n^k/k! tends to one regardless of the word. The exact count therefore bounds every length-n density by 1+o(1) times the following quantity, where o(1) tends to zero as n grows:

\frac{k!}{r!}\max_{0\le p\le1}p^{r+1}(1-p)^r\ \max_\mu F_r(\mu).
Rounding the optimal zero count at each length and using the quantiles of a maximizing distribution attains this value in the limit. The first factor to optimize has a unique maximum at p=(r+1)/(2r+1). The second determines where to put the ones among those zeros.

Why a fixed number of blocks falls short

For r\ge2, a placement distribution that gives the maximum cannot concentrate a positive share of the ones at a single coordinate. Such a concentration is called an atom. The reason is that two ones drawn at an atom have no zero between them. Dividing its mass equally between two nearby positions opens a gap for those pairs. When just one draw comes from the atom, the gains and losses cancel to first order.

Here are the details. Uniform placements have positive mean gap product. To do at least as well, a distribution must have at least r distinct interior points in its support (points whose every neighborhood has positive probability). At the maximum it must also have no mass at either endpoint: draws that hit an endpoint have product zero, so removing any such mass and rescaling the rest to total mass one would increase the mean.

Suppose a maximizing distribution has an atom at an interior point t. Move each draw that hits t independently to t-\epsilon or t+\epsilon with equal probabilities. For each original set of draws there are three possibilities. If no draw hits t, nothing changes. If just one hits, sufficiently small displacements preserve its order relative to all the other draws. The gap product is then a polynomial in that displacement, even if other draws tie. Averaging the two signs cancels the linear term. This cancellation carries over to the mean over all single hits: moving each coordinate by at most \epsilon moves each sorted coordinate by at most \epsilon, so each gap changes by at most 2\epsilon. Since gaps lie in [0,1], their product changes by at most 2(r+1)\epsilon. Dominated convergence therefore lets us average the change divided by \epsilon and take its limit. The mean change from single hits is o(\epsilon), negligible compared with \epsilon.

If two or more draws hit t, the old product is zero and the change can only be positive or zero. With positive probability exactly two hit and move to opposite sides, while the other r-2 draws fall in disjoint small neighborhoods of other interior support points. No other draws are needed for r=2. Choose those neighborhoods separated from each other, from t and from the endpoints. The new gap has length 2\epsilon and all other gaps stay bounded away from zero. On this event the gain is at least a positive constant times \epsilon. It outweighs the single-hit change, contradicting maximality.

A word with at most R runs of ones has at most R placement coordinates. Distributions on at most R points form a compact set because both the positions and the weights vary in compact sets. Their best mean gap product is attained, so the absence of atoms at the unrestricted maximum makes the gap between the two maxima strictly positive. Moreover, in any sequence of words approaching the best density, the letter fractions must approach their optimal values and every converging subsequence of placement distributions must have a limit maximizing F_r. If one run of ones kept a positive fraction of the word along a subsequence, its coordinate would have a further converging subsequence, leaving an atom at the limit. Thus even the longest run of ones occupies a vanishing fraction of the word.

The optimal placement for 01010

For 01010 there are just two ones to place. With their coordinates in order, x\le y, the product of the three gaps is K(x,y)=x(y-x)(1-y). Define K(y,x)=K(x,y) for the reverse order. For a distribution \mu, write U_\mu(x)=\int K(x,y)\,d\mu(y) for the mean product with one placement fixed at x. We call this the potential at x. Its average over the first placement, E(\mu)=\int U_\mu(x)\,d\mu(x)=F_2(\mu), is the energy, the quantity we want to maximize.

A site with higher potential offers a greater return per placement. On each of nine evenly spaced grids we sought a distribution with equal potential at all occupied sites and no higher potential elsewhere. On the finest grid the occupied sites ran from 112/512 to 400/512. Except at the two boundary sites, multiplying each site's weight by x^2(1-x)^2 gave values within a factor of 1.000011 of one another. This suggested that across a single interval the density was proportional to 1/[x^2(1-x)^2].

Write h(x)=x(1-x) and let a be the solution of

(1-a)\log((1-a)/a)=1,\qquad 0<a<1/2 .
The left side decreases strictly from infinity to zero. The proposed placement density, per unit of zero coordinate, is
f(x)=\frac{h(a)}{2h(x)^2}\,\mathbf1_{[a,1-a]}(x).
The indicator makes the density zero outside [a,1-a]. Use three zeros per two ones overall, the optimal proportions found above. The quantile construction now specifies finite words approaching this profile. Since a=0.2178117057\ldots of the zeros precede the mixed interval and the same fraction follow it, each outer zero block occupies a fraction 3a/5 of the whole word in the limit.

Equal potential across the mixed interval

Replacing a fraction s of a distribution \mu by mass at x changes its energy to first order by 2s[U_\mu(x)-E(\mu)]. Thus a maximizing distribution must place all its mass where the potential reaches its maximum. To find a density f with this property, write U for its potential. On an interval where the density is continuous, differentiate the two formulas for K(x,t), one for t<x and one for t>x. This gives

\tfrac12 U''(x)=h(x)f(x)-D(x),\qquad D(x)=\int_0^x t f(t)\,dt+\int_x^1(1-t)f(t)\,dt .
The first term comes from the jump in the first derivative of K at x=t. Since D'=(2x-1)f, constant potential requires (h^2 f)'=0. For a symmetric density on [a,1-a] with total mass one, the left edge must satisfy h(a)f(a)=D(a)=1/2. This determines the numerator of the proposed density; its total mass determines a.

To check the mass, an antiderivative of 1/h^2 is 1/(1-x)-1/x+2\log(x/(1-x)). Integrating f therefore gives 1-2a+2h(a)\log((1-a)/a)=1. Symmetry now gives D(a)=1/2, equal to h(a)f(a) on the interior side of the edge. Throughout the interior, (hf)'=(2x-1)f=D', so hf=D there and U''=0. The potential is linear on that interval and symmetric about its midpoint, so it is constant. To find its value, evaluate it from the left. For x\le a,

U(x)=x\int h(t)f(t)\,dt-x^2\int(1-t)f(t)\,dt=ax-x^2/2,
where the first integral is h(a)\log((1-a)/a)=a. At x=a this gives the plateau height a^2/2. The potential is strictly lower to the left of the interval and, by reflection, to the right.

A bound for every placement distribution

Equal potential on the occupied interval rules out a first-order improvement. To compare the profile with entirely different distributions, we use a geometric identity for the gap product. Represent each coordinate x by the function v_x(s)=\mathbf1_{s\le x}-x, where the indicator is one for s\le x and zero otherwise. Treat these functions as vectors in L^2[0,1], the space of square-integrable functions with inner product given by integrating their product. Then \langle v_x,v_y\rangle=G(x,y)=\min(x,y)-xy. The squared length of v_x is h(x), and the gap product satisfies

K(x,y)=h(x)h(y)-G(x,y)^2 .
The right side is the determinant of the two-by-two matrix of inner products, or geometrically the squared area spanned by the two vectors. Related methods for choosing experimental designs maximize the expected squared volume of a simplex with independent random vertices in Euclidean space [4]. Here the origin is fixed and the two vectors lie on the curve x\mapsto v_x.

For any placement distribution \nu, let b_\nu=\int h\,d\nu and T_\nu(s,t)=\int v_x(s)v_x(t)\,d\nu(x). Averaging the determinant over both placements gives E(\nu)=b_\nu^2-\|T_\nu\|_2^2, where the squared norm integrates T_\nu(s,t)^2 over the unit square. If instead one placement comes from \mu and the other from \nu, the same identity gives the following bound when both energies are positive:

\iint K\,d\mu\,d\nu = b_\mu b_\nu-\langle T_\mu,T_\nu\rangle \ \ge\ \sqrt{E(\mu)E(\nu)} .
Indeed, the Cauchy–Schwarz inequality bounds the inner product above by the product of the norms. Write (b,u)=(b_\mu,\|T_\mu\|_2) and (c,v)=(b_\nu,\|T_\nu\|_2). Positive energies give bc-uv>0; squaring the remaining inequality reduces it to (bv-cu)^2\ge0.

Now take \mu to be the proposed distribution with density f. Its energy is the plateau height a^2/2. The mean product with a second placement from any distribution \nu is at most that height, since it is an average of the potential of \mu. The bound above then gives E(\nu)\le E(\mu)=a^2/2, which also holds for zero energy. Substituting the optimal letter fractions in the counting formula gives the sharp limiting probability

\boxed{60(3/5)^3(2/5)^2\,a^2/2=0.049187802509\ldots .}

Equality determines the profile uniquely. Any other optimal distribution \nu must lie entirely on the plateau to obtain its full height. Equality in both steps of the determinant bound requires T_\nu/b_\nu=T_\mu/b_\mu. Since the energies are equal and positive, the energy formula then gives b_\nu=b_\mu and T_\nu=T_\mu.

To see why this identifies the placements themselves, take smooth functions g,l that vanish outside closed intervals in (0,1). Integrating v_x against the derivative g' gives \langle v_x,g'\rangle=g(x). Integrating T_\nu(s,t) against g'(s)l'(t) therefore gives \int gl\,d\nu. Choose l equal to one on the plateau. The equality of the two functions T now says that every such g has the same integral under the two distributions, so \nu=\mu. All sequences approaching the maximum have this same limiting distribution of ones among the zeros, though their individual letters can be ordered differently.

The cost of mixing uniformly

Even the best uniformly mixed interval between two zero blocks gives only 4.89543%, compared with 4.91878% for the optimal profile. Let L be the width of a uniform interval on the zero coordinate, with exterior gaps of lengths b,c summing to 1-L. Integrating x(y-x)(1-y) over two uniform placements in the interval, accounting for both orders, gives

E=Lbc/3+L^2(b+c)/12+L^3/60.
At fixed L, equal outer gaps maximize bc. The energy then becomes L/12-L^2/12+L^3/60, largest at L=(5-\sqrt{10})/3. The optimal nonuniform profile puts more ones near each outer block while keeping the center mixed (Figure 1).

Local fraction of ones along the word for the optimal profile and the best uniformly mixed interval.

Figure 1. Both constructions use three zeros per two ones. The horizontal axis measures position in the whole word, counting both letters. Zero coordinate x corresponds to position 3x/5+(2/5)\int_0^x f(t)\,dt. The optimal construction uses 13.07% of its length at each end for zeros. The steep rise at the start of mixing marks a change in the local fraction of ones; zeros are still present throughout the mixed region.

Bounds calculated from individual words

A word of length 500,000 bounds the limiting probability to within 3.2\times10^{-9}. We used the endpoint equation for a to construct the word, but the bound can be checked from its letters alone. For any word with the optimal letter fractions and at least one copy, let e be the energy of its observed placement distribution and u the maximum of its potential anywhere on the zero coordinate. Repeating each letter equally many times produces arbitrarily long words with the same placements and energy e. This gives a lower bound on the optimal energy. For the upper bound, the mean product with any other distribution is at most u, so the determinant inequality bounds the optimal energy above by u^2/e. Multiplying the two energy bounds by 60(3/5)^3(2/5)^2 converts them to probabilities.

Word length Lower bound on limiting probability Upper bound
500 0.049184986076 0.049267318266
5,000 0.049187775607 0.049189405346
50,000 0.049187802248 0.049187905118
500,000 0.049187802506 0.049187805665

Table 1. Bounds rounded outward. The lower bound is 5! times the copy count divided by the fifth power of word length. Dividing the copy count by \binom{n}{5} instead gives the finite word's density, which need not bound the limiting probability from below.

For longer odd alternating patterns, splitting a block still proves a positive gap for every fixed bound on the number of runs. The argument leaves open how quickly that gap closes as more runs are allowed and what placement profile is optimal beyond five letters.

Computational methods

To count copies in finite words, we extended each word by each possible letter. Appending a letter can only create copies of patterns ending in that letter. The number of new copies equals the old copy count for the pattern with its last letter removed (the empty pattern counts once). For each length and pattern we recorded the maximum count, the number of words attaining it and the fewest runs among them. For a separate check of the gap formula, we generated words through length 14 by writing each positive integer through 32767 in binary [5] and removing its initial 1. For all 97,632 applicable pairs of words and zero-starting alternating patterns of lengths five, seven and nine, summing the products of integer gap sizes over choices of ones agreed with the count from appending letters.

For the grid calculations we tried symmetric intervals of consecutive sites. Within each interval we solved the linear equations that give equal potential at all its sites, scaled the weights to sum to one and checked that none was negative. We checked the potential at every grid site. The subsequent proof compares the resulting continuous profile with every competing distribution, whatever its symmetry or support.

For Table 1 we solved the endpoint equation by bisection. At each cell boundary on the zero grid we integrated the proposed density up to that point and rounded the cumulative number of ones to the nearest integer. Differences between successive rounded totals gave the number of ones to insert in each gap. A separate calculation used only the resulting word. It counted copies by the appending rule and recovered the placement distribution from the letters. Its potential is quadratic between neighboring grid positions, so we evaluated both ends and any interior vertex to find the maximum over the full interval. All counts and bounds calculated from the word used exact arithmetic.

References

  1. K. Menon and A. Singh. Subsequence frequency in binary words, §4, Corollary 4.2 and following discussion (2023).
  2. A. Burstein, P. Hästö and T. Mansour. Packing patterns into words, §1 (2003).
  3. G. Bruda. Enumerating binary words restricted by subsequence frequency (2026).
  4. L. Pronzato, H. Wynn and A. Zhigljavsky. An extended generalised variance, with applications (2015 preprint version).
  5. OEIS Foundation. A007088: The binary numbers, table of binary representations.