# Extremes after Merging Two Digits — Solution ## Method and Contributions The preferred method is the setter's linear greedy selection of the higher digit and then its partner, distinguishing carry and no-carry cases. Codex (GPT-6) supplies the comparison proof, boundary analysis and portable C++17 implementation below. Lexicographic comparison of equal-length digits is the usual positional-notation property. The diagram shows sample one in base ten. The two arrows give the choices producing the maximum and minimum. ![ Worked example ](assets/example.svg) ## Comparing Results After removing leading zeros, compare lengths first and digits second. The result stays positive: a no-carry operation preserves the digit sum, while a carry leaves a digit one. A carry retains all $n$ digits. A no-carry operation leaves at most $n-1$ . Thus, maximize by carrying whenever possible, and minimize without carrying whenever possible. Positions below refer to the original array. For a fixed left position $i$ , a no-carry merge increases the right digit, so minimization uses the rightmost feasible partner. A carry decreases the right digit, so maximization also uses the rightmost feasible partner. Delaying the change preserves favorable earlier digits. If the added value is zero, choosing a later partner remains no worse. ## Maximum Scan from right to left with a suffix maximum to find the rightmost position $i$ that can carry with a later digit. When $a_i\ge2$ , replacing it by one is the first decrease. Among such operations, a later position is better; for that position choose the rightmost feasible partner. Handle $a_i=1$ separately. It can carry only with $k-1$ , leaves the higher digit unchanged, and changes the lower digit to zero. Use the leftmost one and the rightmost $k-1$ , provided they occur in that order. Let the latter position be $q$ . If $q>i$ , this operation delays the first decrease beyond the ordinary candidate and wins. If $q=i$ , the ordinary candidate leaves one there while this candidate leaves zero, so the ordinary candidate wins. If $q2$ , deleting the first digit also removes at least one leading zero. This beats every result retaining $n-1$ digits. Use the rightmost feasible partner. After excluding that case, feasible no-carry results have $n-1$ digits. A pair satisfying $$a_i>a_{i+1},\qquad j\ge i+2,\qquad a_i+a_j