Exploring Strings for Which Only One Character Comes Lexicographically After Its Neighbour to the Left
Taking three different letters from the 26 letters of the alphabet, characters strings of length 3 can be formed. For each length n where 1 <= n <= 26, let p(n) be the number of strings of length n...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Taking three different letters from the \(26\) letters of the alphabet, character strings of length three can be formed.
Examples are ’abc’, ’hat’ and ’zyx’.
When we study these three examples we see that for ’abc’ two characters come lexicographically after its neighbour to the left.
For ’hat’ there is exactly one character that comes lexicographically after its neighbour to the left. For ’zyx’ there are zero characters that come lexicographically after its neighbour to the left.
In all there are \(10400\) strings of length \(3\) for which exactly one character comes lexicographically after its neighbour to the left.
We now consider strings of \(n \le 26\) different characters from the alphabet.
For every \(n\), \(p(n)\) is the number of strings of length \(n\) for which exactly one character comes lexicographically after its neighbour to the left.
What is the maximum value of \(p(n)\)?
Problem 158: Exploring Strings for Which Only One Character Comes Lexicographically After Its Neighbour to the Left
Mathematical Analysis
Setup
We choose distinct letters from 26, then arrange them in a sequence of length such that exactly one position () satisfies (an “ascent”).
Counting with Eulerian numbers
The number of permutations of elements with exactly ascents is the Eulerian number .
The Eulerian numbers satisfy:
with .
We want exactly 1 ascent, so we need .
Formula for one ascent
The Eulerian number for exactly one ascent is:
Proof by induction: Base case . Inductive step:
Choosing the letters
Since we choose letters from 26, and the relative order is determined by the permutation:
Maximizing
We compute for each from 1 to 26 and find the maximum. Note:
- (no pair to compare)
- The maximum occurs at some intermediate value of .
Computing shows is maximized at with:
Correctness
Theorem. The method described above computes exactly the quantity requested in the problem statement.
Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer.
Complexity
to compute all values.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
int main() {
// Compute C(26, n) * (2^n - n - 1) for n = 1..26, find max
// Use __int128 to avoid overflow in intermediate computation
// Precompute C(26, n)
ll C[27];
C[0] = 1;
for (int i = 1; i <= 26; i++) {
C[i] = C[i-1] * (27 - i) / i;
}
ll best = 0;
for (int n = 1; n <= 26; n++) {
ll euler1 = (1LL << n) - n - 1; // 2^n - n - 1
ll pn = C[n] * euler1;
best = max(best, pn);
}
cout << best << endl;
return 0;
}
from math import comb
def p(n):
"""Count n-length strings from n distinct letters (out of 26) with exactly one ascent."""
if n <= 1:
return 0
eulerian_1 = 2**n - n - 1
return comb(26, n) * eulerian_1
# Find the maximum p(n) for 1 <= n <= 26
values = [(n, p(n)) for n in range(1, 27)]
best_n, best_val = max(values, key=lambda x: x[1])