JŒ!nJ$S⁼ɗƇ2ị⁸Ṁµ¡
Try it online!
A dyadic Link accepting a list of digits (as non-negative integers) on the left and a non-negative integer on the right which yields a list of digits.
How?
JŒ!nJ$S⁼ɗƇ2ị⁸Ṁµ¡ - Link: L, n µ¡ - repeat the monad to the left n times -- i.e. f(f(f(...(L)...))): J - range of length Œ! - all permutations Ƈ - filter keep if: ɗ 2 - last three links as a dyad with right argument 2 $ - last two links as a monad: J - range of length n - not equal? (vectorises) S - sum ⁼ - equal? ị - index into (vectorises): ⁸ - chain's left argument, L Ṁ - maximum
20 byte version which is fine within the previous time-constraint since \$\binom{18}2=\frac{18\times 17}2=153\$
JŒcœṖḢ;Ḣ€ṚżƊƊFɗ€⁸Ṁµ¡
Try it online!
How?
JŒcœṖḢ;Ḣ€ṚżƊƊFɗ€⁸Ṁµ¡ - Link: L, n µ¡ - repeat the monad to the left n times -- i.e. f(f(f(...(L)...))): J - range of length Œc - all (length(L)-choose-2) pairs: [[1,2],[1,3],[1,4],...,[2,3],[2,4],...] € - for each (such pair, P): ɗ ⁸ - last three links as a dyad, with right argument L: œṖ - partition (L) at indexes (P) - call this X Ɗ - last three links as a monad - i.e. f(X): Ḣ - head (of X) (the items up to but not including the first to swap) Ɗ - last three links as a monad - i.e. f(X): Ḣ€ - head each (actually removes them too) (the items to swap) Ṛ - reverse ż - zip with (the altered X) ; - concatenate F - flatten Ṁ - maximum
[code-golf]+[restricted-time]challenges lately.. They are complete opposites imho. Anyway, as for the actual challenge, if we are to output huge test cases withk=18andnbetween \$10^{17}\$ and \$10^{18}\$ within 10 seconds, you may want to add some test cases for those. \$\endgroup\$kis larger than the length ofnwould be good as well, since we have to do exactlykswaps instead of up tokswaps. Can't really think of a good test case for this, but I think we might need to do an inefficient swap first sometimes in order to get to the maximum in exactlykswaps. \$\endgroup\$nas a list of digits? \$\endgroup\$n=501, k=2would become510in the first iteration, but then should be swapped back to501as our result. \$\endgroup\$