taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.2.10-20

Exercise 1.2.10-20

not stated yet in Lean solution written

RatingM22
MarkersM (mathematical)
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 1.2.10 of Volume 1. Companion chapter: Analysis of an algorithm

The task

Let aa be a permutation and b1≤⋯≤bnb_1\le\cdots\le b_n. Let mLm_L be the largest ak−bka_k-b_k over the left-to-right maxima of aa, and mRm_R the largest bk−akb_k-a_k over the right-to-left minima. Show that max⁡∣ak−bk∣=max⁡{mL,mR}\max|a_k-b_k|=\max\{m_L,m_R\}.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Show that the largest ak−bka_k-b_k is reached at a left-to-right maximum of aa, and the largest bk−akb_k-a_k at a right-to-left minimum.

Hint 2

Use the smallest, respectively the largest, index with the largest value, and the monotonicity of bb.

Solution

Show the solution

Call aka_k a left-to-right maximum when ak>aja_k>a_j for all j<kj<k, and a right-to-left minimum when ak<aja_k<a_j for all j>kj>k. The element a1a_1 is the first kind and ana_n is the second kind, so mLm_L and mRm_R are defined.

Claim 1. max⁡k(ak−bk)=mL\max_k(a_k-b_k)=m_L. Let d=max⁡k(ak−bk)d=\max_k(a_k-b_k) and let kk be the smallest index with ak−bk=da_k-b_k=d. If aka_k were not a left-to-right maximum, there would be j<kj<k with aj≥aka_j\ge a_k. Then bj≤bkb_j\le b_k gives aj−bj≥ak−bk=da_j-b_j\ge a_k-b_k=d, so jj also attains the maximum, which contradicts the choice of kk. So aka_k is a left-to-right maximum and mL≥dm_L\ge d. Also mL≤dm_L\le d, because mLm_L is a maximum over a subset. So mL=dm_L=d.

Claim 2. max⁡k(bk−ak)=mR\max_k(b_k-a_k)=m_R. Let e=max⁡k(bk−ak)e=\max_k(b_k-a_k) and let kk be the largest index with bk−ak=eb_k-a_k=e. If aka_k were not a right-to-left minimum, there would be j>kj>k with aj≤aka_j\le a_k. Then bj≥bkb_j\ge b_k gives bj−aj≥bk−ak=eb_j-a_j\ge b_k-a_k=e, which contradicts the choice of kk. So mR=em_R=e.

Finally max⁡k∣ak−bk∣=max⁡(max⁡k(ak−bk),max⁡k(bk−ak))=max⁡{mL,mR}\max_k|a_k-b_k|=\max\bigl(\max_k(a_k-b_k),\max_k(b_k-a_k)\bigr)=\max\{m_L,m_R\}.

Check

Take a=(5,1,4)a=(5,1,4) and b=(2,3,6)b=(2,3,6). The differences ak−bka_k-b_k are 3,−2,−23,-2,-2 and bk−akb_k-a_k are −3,2,2-3,2,2, so max⁡∣ak−bk∣=3\max|a_k-b_k|=3. The left-to-right maxima of aa are 55 only (at k=1k=1), so mL=3m_L=3. The right-to-left minima are 44 (at k=3k=3) and 11 (at k=2k=2), so mR=max⁡(6−4,3−1)=2m_R=\max(6-4,3-1)=2. And max⁡{3,2}=3\max\{3,2\}=3.

Remarks

Only the records of aa matter, and there are HnH_n of them on average.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.