let n be the 0 based length of your set (for ABCDE n = 4) let x be the 0 based position of the first element (for 'CD' x = 2) let y be the 0 based position of the second element (for 'CD' y = 3) The position in the outputed combination would be: [n(n+1)-x(x+1)]/2 + y-x so for 'CD': [4(4+1) - 2(2+1)]/2 + 3 - 2 ==> [20 - 6]/2 + 1 = 8 (solution is not 0 based)