Code Golf
5 years ago Mathgeek

Now to start thinking laterally!

For an array of N distinct numbers in increasing order, there are N! ways to permute that array. Those permuted arrays can then also be sorted, prioritizing whichever array has the smallest element in the first position, then breaking ties with the second position, etc.

The input is an array and a number M with 0 <= M < N! , and the output should be, if one sorted all N! permutations of the original array, the Mth element of that list of permutations. So, M = 0 would give your original sorted array, and M=N!-1 would be the array in reverse.

Input Output
[1, 2, 3, 4], 5 [1, 4, 3, 2]
[1, 4, 7], 2 [4, 1, 7]
[3, 4, 6, 9, 10], 114 [10, 9, 3, 4, 6]
[1, 2, 3], 0 [1, 2, 3]
J J, 2 bytes
5 years ago Bubbler
Jelly, 3 bytes
4 years ago xigoi
Top Answer J
5 years ago Bubbler

J, 2 bytes

Try it online!

Sorry for the boring answer, but this is a bulit-in function which computes the nth permutation of the given array in the increasing order.

Answer #2
4 years ago xigoi

Jelly, 3 bytes

Try it online!

Jelly has a built-in for this (œ?), but it indexes from 1, so the number has to be incremented ().

5 years
Mathgeek — Monday, 3rd Feb 2020 00:29

an array of N distinct numbers in increasing order

an hour
Bubbler — Sunday, 2nd Feb 2020 23:01

Can we assume the elements of the array are distinct?

15 hours
Mathgeek — Sunday, 2nd Feb 2020 07:49

N is inferred from array length. I like the reworked wording. Thanks!

5 hours
xnor — Sunday, 2nd Feb 2020 02:51

@Mathgeek

xnor — Sunday, 2nd Feb 2020 02:50

May we take N as input as well, or does that have to be inferred from the array length?

xnor — Sunday, 2nd Feb 2020 02:50

Nice challenge! I tried cleaning up some wording. Feel free to change stuff back if you don’t like it.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.