Maths & numbers

Combination rank and unrank calculator in lexicographic order

This calculator indexes fixed-size subsets in ordinary lexicographic order.

Your numbers

Use ordinary lexicographic order, not colex order.
Element labels are 1 through n; n=0–500.
Integer 0–min(n,100).
Exactly k strictly increasing labels, or blank when k=0.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

This calculator indexes fixed-size subsets in ordinary lexicographic order. Elements carry labels one through n, while the subset index starts at zero. The number of subsets is a binomial coefficient, but finding or reconstructing a specific indexed subset requires keeping track of lexicographic prefix blocks. Both directions share the same convention so round trips can be checked without generating the entire subset list.

THE FORMULAAt position j, choosing element x leaves C(n−x,k−j) completions. Sum earlier prefix blocks for rank; subtract them for unrank.

A worked example

For n=4 and k=2, the order is {1,2},{1,3},{1,4},{2,3},{2,4},{3,4}. The subset {2,3} therefore has rank three. The three subsets beginning with 1 form a block that comes first; within the block beginning with 2, choosing 3 is the first completion. Unranking three follows exactly those two decisions and reconstructs {2,3}.

Good to know

Use n=0–500 and k=0–min(n,100). Subset labels must be strictly increasing integers in 1…n, with no repeats. Ranks are exact zero-based integers below C(n,k). For k=0, the empty subset is the only object and has rank zero. Colexicographic combinadics, bitmask order and Gray order are different conventions and are not substituted.

The first differing element determines lexicographic order

Compare two increasing label tuples from left to right. At their first differing position, the tuple with the smaller element comes first. Thus {1,4} precedes {2,3}, although a colexicographic order based on the largest differing element can place them differently. Naming only a combinatorial number system without specifying its order is not enough to interpret a rank.

For a fixed prefix ending before the next selected element x, all remaining labels must exceed x. If r positions remain after x, their completion count is C(n−x,r). These blocks occur in increasing x order, allowing an exact rank calculation by adding the sizes of blocks that precede the entered element.

Three subsets beginning with one precede the subset two comma three1,201,311,422,332,443,45Three earlier subsets → rank 3
For n=4,k=2, ordinary lexicographic order is 12,13,14,23,24,34. Thus {2,3} has zero-based rank three. Element labels still begin at one.

The Combinations calculator counts all k-subsets, while this page finds or reconstructs one subset at a lexicographic index.

Separate one-based labels from zero-based ranks

Label one denotes the first member of the ground set. Rank zero denotes the first complete k-subset. They are different kinds of index, so there is no contradiction in using both conventions on one page. The field labels state each convention explicitly and the result repeats them beside the reconstructed subset.

Changing the element labels to 0…n−1 without changing the input contract shifts every tuple, even though the total number of subsets is unchanged. Similarly, adding one to a rank changes the selected subset except at an invalid endpoint. If an external library uses one-based ranks, convert that rank before entering it rather than modifying the subset labels.

The empty and full subsets are legitimate endpoints

The empty subset contains no element labels and has rank zero for every supported n. A full subset with k=n is also unique and has rank zero. The k≤100 input limit means the full case is supported only when n≤100. These endpoints are handled directly instead of being rejected as missing selections.

A large n can produce a count far beyond safe floating-point integers even for a moderate k. Completion counts and ranks therefore remain exact integers. Unranking skips whole blocks rather than enumerating each earlier subset. Exhaustive small-ground-set comparisons and rank/unrank round trips test both the ordering and the boundary conventions.

A few questions, answered

Is this the usual combinadic colex rank?

No. It uses ordinary left-to-right lexicographic order. A colex rank needs a separate conversion before it can identify the same subset here.

What should I enter for k=0?

Leave the subset field blank in rank mode. In unrank mode enter rank zero. The result is the unique empty subset.

Find your calculator

Start typing to explore the library.