For up to about 11 items in the set, then usually the fastest approach is to generate all the combinations and run the calculation in vectorized form over all of them, choosing the one that gives the best result.
Once you get to roughly 12 then the number of possibilities starts becoming large enough that more complex approaches start winning.
Using combinations of integers is a form of integer programming. MATLAB used to have a separate integer-only programming function, but it was replaced by "mixed integer" routines, which are routines which can take a mix of integer constraints and continuous bound constraints. https://www.mathworks.com/help/optim/ug/mixed-integer-linear-programming-algorithms.html
The difficulty with intlinprog() is that it has real way to say that only combinations (or permutations) are to be used, as opposed to simply integers within a range but which might be repeated. But if your function works with ordered inputs, then it is possible to program it by using the A, b, matrices, where for each adjacent pair of values you would configure weights 1 for x(K) and -1 for x(K+1) and have -1 in the corresponding element of b: x(K)-x(K+1) <= -1 is true for integer x only if the values are sorted in increasing order.
