Lexicographic Permutations
A permutation is an ordered arrangement of objects. The lexicographic permutations of {0, 1, 2} are: 012, 021, 102, 120, 201, 210. What is the millionth lexicographic permutation of the digits {0,...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are: $$012 \quad 021 \quad 102 \quad 120 \quad 201 \quad 210$$ What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
Problem 24: Lexicographic Permutations
Mathematical Development
Definitions
Definition 1 (Factorial Number System). The factoradic (or factorial number system) represents a non-negative integer as
where the digits satisfy for each .
Definition 2 (Lehmer Code). Given a permutation of , its Lehmer code is the sequence where counts elements to the right of position that are smaller than .
Theorems and Proofs
Theorem 1 (Factoradic Uniqueness). Every non-negative integer with has a unique factoradic representation satisfying for each .
Proof. We proceed by strong induction on .
Base case (): The only value is , represented by the empty sequence.
Inductive step: Assume the claim holds for all factorials up to . Given , apply the division algorithm: , where and . Since , we have . Since , the inductive hypothesis yields a unique representation of as . Uniqueness of the full representation follows from the uniqueness of quotient and remainder in the division algorithm.
Theorem 2 (Lehmer Code Bijection). There is a bijection between and the permutations of elements in lexicographic order, mediated by the factoradic representation. Specifically, if , then the -th permutation (0-indexed) is constructed by: at step (), select the element of rank among the remaining elements.
Proof. By induction on .
Base case (): The single permutation corresponds to .
Inductive step: Partition the permutations into blocks of size each, where block contains all permutations beginning with the -th smallest element (0-indexed). The permutation at global index lies in block , and has local index within that block. By the inductive hypothesis, the remaining positions are determined by the factoradic digits applied to the remaining elements. Since the block structure respects lexicographic order and the inductive step preserves it, the full construction produces the -th permutation.
Lemma 1 (Feasibility). The millionth permutation corresponds to 0-based index , and , so the factoradic representation exists with .
Proof. Immediate from .
Computation
Applying Theorem 2 with and :
| Step | Remainder | Available digits | Selected | |||
|---|---|---|---|---|---|---|
| 0 | 999999 | 362880 | 2 | 274239 | {0,1,2,3,4,5,6,7,8,9} | 2 |
| 1 | 274239 | 40320 | 6 | 32319 | {0,1,3,4,5,6,7,8,9} | 7 |
| 2 | 32319 | 5040 | 6 | 2079 | {0,1,3,4,5,6,8,9} | 8 |
| 3 | 2079 | 720 | 2 | 639 | {0,1,3,4,5,6,9} | 3 |
| 4 | 639 | 120 | 5 | 39 | {0,1,4,5,6,9} | 9 |
| 5 | 39 | 24 | 1 | 15 | {0,1,4,5,6} | 1 |
| 6 | 15 | 6 | 2 | 3 | {0,4,5,6} | 5 |
| 7 | 3 | 2 | 1 | 1 | {0,4,6} | 4 |
| 8 | 1 | 1 | 1 | 0 | {0,6} | 6 |
| 9 | 0 | 1 | 0 | 0 | {0} | 0 |
Editorial
We construct the required permutation directly from the factoradic representation of . At each position we compute the relevant factorial block size, choose the corresponding index among the remaining digits, append that digit to the answer, and remove it from the available set. This is sufficient because lexicographic permutations are partitioned into equal factorial-sized blocks at each step.
Pseudocode
Algorithm: Lexicographic Permutation by Factoradic Selection
Require: An ordered digit set D and an index k ≥ 1.
Ensure: The k-th permutation of D in lexicographic order.
1: Set k' ← k - 1 and let R be the ordered list of available digits.
2: For each position from most significant to least significant do:
3: Let m be the number of digits that will remain after this position, and compute q ← floor(k' / m!).
4: Append the q-th element of R to the output, remove it from R, and replace k' ← k' mod m!.
5: Return the constructed permutation.
Complexity Analysis
Proposition. Algorithm KthPermutation runs in time and space.
Proof. The main loop iterates times. In each iteration, the dominant cost is the removal of an element from the list available, which takes time with an array-based list (shifting subsequent elements). All other operations (factorial lookup, integer division, modular reduction) are per iteration. Total: .
Space consists of the available list and result, each of size .
Remark. Using a balanced order-statistic tree (e.g., an augmented AVL or red-black tree), the select-and-delete operation can be performed in per step, reducing the total time to .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
const int n = 10;
int k = 1000000 - 1;
vector<int> fact(n, 1);
for (int i = 1; i < n; i++)
fact[i] = fact[i - 1] * i;
vector<int> digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
string result;
for (int i = 0; i < n; i++) {
int f = fact[n - 1 - i];
int idx = k / f;
k %= f;
result += to_string(digits[idx]);
digits.erase(digits.begin() + idx);
}
cout << result << endl;
return 0;
}
import math
def solve():
k = 1000000 - 1
digits = list(range(10))
result = []
for i in range(10):
f = math.factorial(9 - i)
idx, k = divmod(k, f)
result.append(digits.pop(idx))
print(''.join(map(str, result)))