In combinatorics, probability theory, and computer science, the problem of the greatest alternating subsequence is to find a subsequence of the greatest length of a given sequence such that its elements are arranged in an alternating manner.
Let be {\ displaystyle \ mathbf {x} = \ {x_ {1}, x_ {2}, \ ldots, x_ {n} \}}
Is a sequence of different real numbers, then a subsequence {\ displaystyle \ {x_ {i_ {1}}, x_ {i_ {2}}, \ ldots, x_ {i_ {k}} \}}
alternating if
- {\ displaystyle x_ {i_ {1}}> x_ {i_ {2}} <x_ {i_ {3}}> \ cdots x_ {i_ {k}} \ qquad {\ text {and}} \ qquad 1 \ leq i_ {1} <i_ {2} <\ cdots <i_ {k} \ leq n.}

Similarly {\ displaystyle \ mathbf {x}}
reverse alternating if
- {\ displaystyle x_ {i_ {1}} <x_ {i_ {2}}> x_ {i_ {3}} <\ cdots x_ {i_ {k}} \ qquad {\ text {and}} \ qquad 1 \ leq i_ {1} <i_ {2} <\ cdots <i_ {k} \ leq n.}

Let be {\ displaystyle {\ rm {as}} _ {n} (\ mathbf {x})}
denotes the length (number of elements) of the largest alternating subsequence of the sequence {\ displaystyle \ mathbf {x}}
. For example, if we consider some permutation of the numbers 1,2,3,4,5, we get
- {\ displaystyle {\ rm {as}} _ {5} (1,2,3,4,5) = 2}
; - {\ displaystyle {\ rm {as}} _ {5} (1,5,3,2,4) = 4,}
because 1,5,3,4 and 1,5,2,4 and 1,3,2,4 are alternating, and there is no alternating subsequence of more elements; - {\ displaystyle {\ rm {as}} _ {5} (5,3,4,1,2) = 5,}
because 5,3,4,1,2 alternating.
The problem of the greatest alternating subsequence is solved in time {\ displaystyle O (n)}
where {\ displaystyle n}
Is the length of the original sequence.
If a {\ displaystyle \ mathbf {x}}
- random permutation of numbers {\ displaystyle 1,2, \ ldots, n}
and {\ displaystyle A_ {n} \ equiv {\ rm {as}} _ {n} (\ mathbf {x})}
, then it can be shown that
- {\ displaystyle E [A_ {n}] = {\ frac {2n} {3}} + {\ frac {1} {6}} \ qquad {\ text {and}} \ qquad \ operatorname {Var} [A_ {n}] = {\ frac {8n} {45}} - {\ frac {13} {180}}.}
![{\displaystyle E[A_{n}]={\frac {2n}{3}}+{\frac {1}{6}}\qquad {\text{and}}\qquad \operatorname {Var} [A_{n}]={\frac {8n}{45}}-{\frac {13}{180}}.}](https://wikimedia.org/api/rest_v1/media/math/render/svg/e426b8cccdd81c463a43ae053b8859614897c6eb)
Moreover, with {\ displaystyle n \ rightarrow \ infty}
random value {\ displaystyle A_ {n}}
centered, normalized, its distribution tends to normal.