I know how to compute the binomial coefficient given n and k (and I use a library for that). Now imagine you store all of those combinations inside an array. Each combination has an index. I don't actually need to store them in an array, but I need to know, if I were to store them, what the array index would be for each combination.
E.g. given an element of the C(n,k) set of possible combinations, I need a function that gives me its index i inside the array, withtout actually creating the whole array. The programming language does not matter, though I need to implement the function in java, but any (pseudo-)language algorithm will do, I will then translate it to java.
For example, in the case of n=5 and k=2, I empirically define this function f(n, k, [a,b]) => N
as:
f(5, 2, [1,2]) = 0
f(5, 2, [1,3]) = 1
f(5, 2, [1,4]) = 2
f(5, 2, [1,5]) = 3
f(5, 2, [2,3]) = 4
f(5, 2, [2,4]) = 5
f(5, 2, [2,5]) = 6
f(5, 2, [3,4]) = 7
f(5, 2, [3,5]) = 8
f(5, 2, [4,5]) = 9
meaning that the (3,5) combination would occupy index 8 in the array. Another example with n=5 and k=3 is f(n, k, [a,b,c]) => N
, empirically defined as
f(5, 3, [1,2,3]) = 0
f(5, 3, [1,2,4]) = 1
f(5, 3, [1,2,5]) = 2
f(5, 3, [1,3,4]) = 3
f(5, 3, [1,3,5]) = 4
f(5, 3, [1,4,5]) = 5
f(5, 3, [2,3,4]) = 6
f(5, 3, [2,3,5]) = 7
f(5, 3, [2,4,5]) = 8
f(5, 3, [3,4,5]) = 9
EDIT for clarification after comments:
In the example above [1,2,3], [2,4,5] and so on, are one of the elements of the C(n,k) set, e.g. one of the possible combinations of k numbers out of n possible numbers. The function needs them in order to compute their index in the array.
However I need to write this function for generic values of n and k and without creating the array. Maybe such a function already exists in some combinatorial calculus library, but I don't even know how it would be called.