Open problems / 060

Sign patterns of subsequences

Two sequences of distinct reals are equivalent when their first differences have the same signs. Take one sequence of length $n$ and count how many classes its subsequences occupy. Kagey found $1, 3, 6, 11$ and wrote “Fibonacci minus 2?”. The guess is right. The maximum is $F_{n+2} - 2$, and the sequences that attain it can be described exactly.

The question

Let $x = (x_1, \dots, x_n)$ be a sequence of distinct real numbers. Its sign word records which way each step goes:

$$w(x) = w_1 w_2 \cdots w_{n-1} \in \{{+},{-}\}^{n-1}, \qquad w_i = \operatorname{sgn}(x_{i+1} - x_i).$$

Two sequences are equivalent when their sign words agree. The question is how many classes the subsequences of a single sequence can meet. Subsequences of length $1$ have the empty sign word and are not counted. Write

$$\mathcal{W}(x) = \{\, w(x_{i_0}, x_{i_1}, \dots, x_{i_k}) : k \ge 1, \; 1 \le i_0 < i_1 < \dots < i_k \le n \,\}, \qquad a(n) = \max_x \, |\mathcal{W}(x)|.$$

Only the relative order of the entries matters, so $x$ may be taken to be a permutation of $[n] = \{1, \dots, n\}$. Kagey's own example is $(0, 1, -1, 2)$. Its subsequences have sign words ${+}{-}{+}$, ${+}{+}$, ${+}{-}$, ${-}{+}$, ${+}$ and ${-}$, and no sequence of length $4$ does better, so $a(4) = 6$.

$a(2), \dots, a(5) = 1, 3, 6, 11$. Is there a general formula? “Fibonacci minus 2?”

Four related questions come with it.

  • R1. What if the entries need not be distinct?
  • R2. What if two sequences are equivalent when they are in the same sort order, meaning the biggest element in the same position, the second biggest in the same position, and so on?
  • R3. What if ${+}{+} \sim {+}$? Kagey conjectures the answer is $2n-3$ for $n \ge 2$.
  • R4. Is the number of classes determined by the number of local minima and maxima?

For R3 with repeated entries allowed he lists the values $1, 4, 8, 15, 25, 40$ for $n = 2, \dots, 7$ and the conjectured values $62, 94, 141, 210, 311$ for $n = 8, \dots, 12$, the last five assuming that $1, 2, 1, 2, \dots$ is optimal. Below, $F_k$ is the Fibonacci sequence with $F_1 = F_2 = 1$, and a subword of $w$ is anything obtained by deleting letters, not necessarily contiguous ones.

The formula

One observation gives the upper bound. For $i < j$ the sign of $x_j - x_i$ has to turn up among the letters $w_i, w_{i+1}, \dots, w_{j-1}$, since otherwise the entries from $x_i$ to $x_j$ would run the wrong way. Choosing one such letter inside each gap of a subsequence, and noting that the gaps are disjoint and increase, gives the following.

Every word in $\mathcal{W}(x)$ is a subword of $w(x)$.

So $a(n)$ is at most the number of distinct nonempty subwords of a binary word of length $n-1$. That number is known. Flaxman, Harrow and Sorkin showed that over any finite alphabet the word that repeats the alphabet cyclically has the most distinct subwords. We need the binary case with its uniqueness, which has a short proof of its own.

A word $w \in \{{+},{-}\}^m$ has at most $F_{m+3} - 1$ distinct subwords, the empty one included, with equality if and only if $w$ is alternating.

Let $G(p)$ count the distinct subwords of the suffix $w_{p+1} \cdots w_m$, so $G(m) = 1$ and $G(m-1) = 2$, and $G$ is strictly decreasing. A nonempty subword of that suffix starting with the letter $c$ is $c$ followed by a subword of the suffix after the first $c$ beyond position $p$, so $G(p) = 1 + \sum_c G(q_c)$ over the letters $c$ that still occur. One of those letters is $w_{p+1}$, with $q_c = p+1$. The other either does not occur or has $q_c \ge p+2$, so $G(p) \le 1 + G(p+1) + G(p+2)$, with equality exactly when $w_{p+2} \ne w_{p+1}$. Induction on $m - p$ gives $G(p) \le F_{m-p+3} - 1$. A single repeat makes one inequality strict, and the strictness propagates down to $G(0)$.

$a(n) = F_{n+2} - 2$ for every $n \ge 1$, and the expanding zigzag $0, 1, -1, 2, -2, \dots$ attains it.

The upper bound is the two lemmas together. For the lower bound, the zigzag has a property the general sequence does not: the sign of $x_j - x_i$ does not depend on $i$ at all. Every earlier entry lies between $-(j-2)/2$ and $(j-1)/2$, and $x_j$ is outside that range on one side, so $\operatorname{sgn}(x_j - x_i) = w_{j-1}$ for every $i < j$. A subword $w_{j_1} \cdots w_{j_k}$ is therefore the sign word of the subsequence at positions $j_1 < j_1 + 1 < j_2 + 1 < \dots < j_k + 1$. Every subword is realized, and there are $F_{n+2} - 2$ nonempty ones.

1234567-3-2-10123positionvalue+−+−+−sign word+−+−+−subwords32 = F(9)−2markedat 2,3,5,7word −−−
The expanding zigzag at $n = 7$, with the sign of each step beside it. Because the sign of $x_j - x_i$ depends only on $j$, every one of the $32$ nonempty subwords of ${+}{-}{+}{-}{+}{-}$ is the sign word of some subsequence. The marked subsequence $1, -1, -2, -3$ realizes ${-}{-}{-}$, although no three consecutive steps of the sequence are all downward.

A001911 lists $F_{k+3} - 2$ from $k = 0$, so $a(n)$ is its term of index $n-1$, and the values for $n = 2, \dots, 12$ are 1, 3, 6, 11, 19, 32, 53, 87, 142, 231, 375. The entry does not mention this problem.

Which sequences attain it

Say that $x$ has a centre $z = x_p$ if $x$ is the union of an increasing subsequence $I$ and a decreasing subsequence $D$ with $I \cap D = \{z\}$. The entries before $z$ then split into an increasing run below $z$ and a decreasing run above it, and the entries after $z$ split the other way. The zigzag has a centre at $x_1$.

If $x$ has a centre, then $\mathcal{W}(x)$ is the set of all nonempty subwords of $w(x)$.

The reason is the same as for the zigzag, split at the centre. To the left of $p$ the sign of $x_j - x_i$ depends only on $i$, and to the right it depends only on $j$. A subword is then realized by taking the positions $j_t$ that lie left of $p$, then $p$ itself, then the positions $j_t + 1$ that lie right of it.

For $n \ge 2$, a permutation $x$ of $[n]$ satisfies $|\mathcal{W}(x)| = a(n)$ if and only if $x$ is alternating and has a centre. There are exactly $2\binom{n-1}{\lfloor (n-1)/2 \rfloor}$ of them.

If $x$ is extremal, then $w(x)$ is alternating and every subword of it is realized. Let $a$ and $b$ count the letters ${+}$ and ${-}$, so $a + b = n-1$. Realizing ${+}^a$ gives an increasing subsequence $I$ with $a+1$ entries and realizing ${-}^b$ gives a decreasing $D$ with $b+1$. Then $|I| + |D| = n+1$ while $|I \cup D| \le n$, so $I$ and $D$ meet. An increasing and a decreasing subsequence meet in at most one entry, so they meet in exactly one and cover $x$, which is a centre. The converse is the proposition. For the count, complementation swaps the two starting letters, so it is enough to count those with $w_1 = {+}$. Each of them has the value $b+1$ as a centre, standing at an odd position, and $x \mapsto I \setminus \{b+1\}$ is a bijection onto the $a$-element subsets of $[n] \setminus \{b+1\}$.

Alternating is necessary and not sufficient. At $n = 4$ there are ten alternating permutations and six extremal ones.

13246 classes14236 classes23146 classes24135 classes34125 classes
The five permutations of $[4]$ with sign word ${+}{-}{+}$, drawn as permutation matrices. The first three are a union of an increasing and a decreasing subsequence meeting in one entry, drawn here as the rising path, the marked falling path and the cell where the two meet, and they reach the maximum of $6$. The last two admit no such pair and reach $5$. All five are alternating and all five have one interior local minimum and one interior local maximum, so the number of classes is not a function of the local extrema.

The count at length $n$ is A063886 at index $n$, so for $n = 2, \dots, 12$ it runs 2, 4, 6, 12, 20, 40, 70, 140, 252, 504, 924. That entry does not mention this problem either. Extremal sequences are not all zigzags. At $n = 4$ the zigzag, its reversal and their complements are four of the six, the missing two being $1324$ and $4231$. At $n = 6$ the permutation $263415$ is extremal with centre $3$, the increasing subsequence $2, 3, 4, 5$ and the decreasing subsequence $6, 3, 1$.

Collapsed runs

Related question R3 collapses every maximal run of equal letters to a single letter, so that ${+}{+}$ and ${+}$ become the same class. Write $\bar u$ for the collapsed form of $u$. Here the count depends on nothing but the number of runs.

Let $x$ have $n \ge 2$ distinct entries and let $w(x)$ have $r$ runs. Then $|\{\bar u : u \in \mathcal{W}(x)\}| = 2r - 1$. The classes are the nonempty alternating words of length at most $r-1$, together with the alternating word of length $r$ starting with $w_1$.

For the lower bound write $c_1, \dots, c_r$ for the letters of the runs of $w(x)$, so that consecutive ones differ. Let $e_0 = x_1$ and let $e_t$ be the last entry of the $t$-th run, so that $x$ is monotone from $e_{t-1}$ to $e_t$ with sign $c_t$. The subsequence $e_s, e_{s+1}, \dots, e_t$ has sign word $c_{s+1} \cdots c_t$, which is already collapsed. Taking $s = 0$ and $s = 1$ produces both alternating words of every length from $1$ to $r-1$, and $s = 0$ with $t = r$ produces one more. For the upper bound, $\bar u$ is an alternating subword of $w(x)$, so its letters come from distinct runs in order.

$a'(n) = 2n-3$ for $n \ge 2$, attained exactly by the alternating permutations. For any $x$, the number of collapsed classes is $2m+1$, where $m$ is the number of interior local extrema of $x$.

That settles R3 as Kagey conjectured, and answers R4 with a yes in the collapsed setting. Without collapsing the answer to R4 is no, already at $n = 4$. The permutations $2143$ and $2314$ both have one interior local minimum and one interior local maximum. But $\mathcal{W}(2143) = \{{+}, {-}, {+}{-}, {-}{+}, {-}{+}{-}\}$ has five elements and $\mathcal{W}(2314)$ has six. The subword ${-}{-}$ of $w(2143) = {-}{+}{-}$ would need a decreasing subsequence of length $3$, and $2143$ has none.

Repeated entries

Now drop distinctness. Sign words are over $\{{+},{-},{=}\}$. Let $b(n)$ be the largest number of distinct sign words of subsequences, which is R1, and $c(n)$ the largest number of distinct collapsed sign words, which is R1 and R3 together.

The eleven values in the problem statement belong to $c$, not to $b$. The page they come from introduces them as being for related question 3 without distinct elements, and they agree with the collapsed counts of $1, 2, 1, 2, \dots$ in all eleven places. The plain counts of that sequence differ from $n = 5$ on.

The subword lemma also fails here, and with it the only upper bound we had. The sequence $1, 2, 1$ realizes the word ${=}$, which is not a subword of ${+}{-}$. Two entries far apart may be equal while no adjacent pair is, so the sign word of the whole sequence no longer controls its subsequences.

The sequence $(1, 2, 1, 2, \dots)$ of length $n \ge 2$ has exactly $F_{n+3} - 3 - \lfloor n/2 \rfloor$ distinct sign words of subsequences. Hence $b(n) \ge F_{n+3} - 3 - \lfloor n/2 \rfloor$.

The sign word of a subsequence depends only on the parities of its indices, and the parity sequences that occur are the nonempty subwords of an alternating binary word of length $n$. There are $F_{n+3}-2$ of those, two of which have length $1$ and give the empty sign word. A sign word containing ${+}$ or ${-}$ determines its parity sequence, and the only words counted twice are the ${=}^k$, of which there are $\lfloor n/2 \rfloor - 1$.

$n$count for 1, 2, 1, 2, …$b(n)$Kagey's value$c(n)$
$2$1111
$3$4444
$4$8888
$5$16161515
$6$28292525
$7$49524040
$8$829262*65
$9$13716294*107
$10$225281141*175
$11$369488210*269

The second column counts the sign words of $1, 2, 1, 2, \dots$ without collapsing, which is the lower bound of the proposition above. Kagey's column is the same sequence counted with its runs collapsed, and the starred entries there are the ones conjectured rather than computed in the problem statement. Everything in the $b$ and $c$ columns is an exhaustive search over all weak orders of length $n$.

For $3 \le n \le 7$ the only maximizers for $c(n)$ are $1, 2, 1, 2, \dots$ and its complement, so Kagey's computed values $1, 4, 8, 15, 25, 40$ are confirmed. At $n = 8$ the sequence $1, 3, 1, 2, 2, 1, 3, 1$ has $65$ collapsed words against $62$, and the gap widens from there. The conjectured values $62, 94, 141, 210$ are refuted. So is $311$ at $n = 12$, without running the search that far: appending one entry to a maximizer for $c(11)$ gives $1, 3, 1, 3, 1, 2, 2, 1, 3, 1, 3, 1$, which has $413$ collapsed words, so $c(12) \ge 413$. That is a lower bound from an explicit witness. The value of $c(12)$ itself was not computed.

2345678910110255075100125length nexcess over 1, 2, 1, 2, ...Kagey's values fail from hereplainb(11) = 488collapsedc(11) = 269
How far the exhaustive maxima run ahead of the sequence $1, 2, 1, 2, \dots$ once entries may repeat. Each curve is measured against that sequence counted the same way, plain against plain and collapsed against collapsed. Both sit at zero and then leave it, the plain count at $n = 6$ and the collapsed count at $n = 8$. Kagey's conjectured values assume the collapsed curve stays at zero, so they fail from $n = 8$ on.

Our own guess failed too. We registered $b(n) = F_{n+3} - 3 - \lfloor n/2 \rfloor$ after seeing $b(2), \dots, b(5)$, and it failed at the first test, $n = 6$. No formula for $b(n)$ or $c(n)$ is proposed, and neither column was in the OEIS as of September 2026. The ratios $b(n)/b(n-1)$ for $n = 8, \dots, 11$ are $1.77$, $1.76$, $1.73$, $1.74$, above the golden ratio that governs $1, 2, 1, 2, \dots$. Some shape is visible in the maximizers. At each length from $8$ to $11$ one plain maximizer uses four distinct values with no two adjacent entries equal, and one collapsed maximizer uses three with exactly one such pair, for instance $1, 4, 1, 3, 2, 3, 2, 3, 1, 4, 1$ and $1, 3, 1, 3, 1, 2, 2, 1, 3, 1, 3$ at $n = 11$. Maximizers on more values occur as well, so the number of distinct values is a property of these examples and not of the whole set.

Sort order

Under R2 two sequences are equivalent when they are order-isomorphic, so the question asks for the largest number $\mathrm{pat}(n)$ of distinct permutation patterns contained in a permutation of length $n$. That is a known problem, posed by Wilf in 2003, who gave a Fibonacci lower bound. Counting patterns of every length from $1$ to $n$, the values are 1, 2, 4, 8, 15, 28, 55, 109, 226, 452, 935, 1949, 3977 for $n = 1, \dots, 13$, which is A088532. Coleman showed $\mathrm{pat}(n)^{1/n} \to 2$, Albert, Coleman, Flynn and Leader showed $\mathrm{pat}(n) \sim 2^n$, and Miller proved

$$2^n - O\bigl(n^2 \, 2^{\,n-\sqrt{2n}}\bigr) \;\le\; \mathrm{pat}(n) \;\le\; 2^n - \Theta\bigl(n \, 2^{\,n-\sqrt{2n}}\bigr).$$

No exact formula is known. We reproduced the first seven terms by brute force and did not pursue this variant further.

What was checked

Two independent programs produced the computed numbers. The first builds sequences from right to left and keeps, for each entry and each length $L$, a bitmask over the $2^L$ or $3^L$ words of that length realized by subsequences starting at that entry. The second enumerates every subsequence of every sequence straight from the definition, and also runs a left-to-right search that stores words as strings. Except where the list below says otherwise, a value counts as checked only when the two agree.

  • $a(n) = F_{n+2} - 2$ by direct enumeration for $n \le 8$ and by the bitmask search for $n \le 10$. Separate runs of the bitmask search alone gave $a(11) = 231$ and $a(12) = 375$, which are the values the theorem predicts.
  • The maximizers are exactly the alternating permutations with a centre for $n \le 10$, with counts 2, 4, 6, 12, 20, 40, 70, 140, 252, and the same runs gave the $504$ and $924$ the theorem predicts at $n = 11$ and $n = 12$.
  • The collapsed count equals $2r-1$ for every permutation of length at most $7$, and $a'(n) = 2n-3$ for $n \le 9$.
  • $b(n)$ and $c(n)$ by direct enumeration for $n \le 6$, by the left-to-right search for $n \le 7$ and by the bitmask search for $n \le 8$. The rows $n = 9, 10, 11$ were produced by both searches in separate runs, the longest taking about an hour and a half. The value $c(11) = 269$ rests on three completed runs.
  • Every formula that started as a guess was written into a ledger, with its predictions for the next few $n$, before the search was run on those $n$. The formula for $a(n)$ and the count of the maximizers both passed that test before they were proved. Two guesses failed it, both in the repeated-entries section.

A script in the repository reruns thirty-one of these checks in about a minute.