Ctrl K

All Orderings

Medium
You get an array values of distinct integers. Return every way to line these numbers up, that is, every permutation of them. Each permutation is a list that contains every number from values exactly once, and no permutation may appear twice. The permutations may be listed in any order.
Example 1
values=[7, -2] → [[7, -2], [-2, 7]]

Two numbers give exactly two orderings.

Example 2
values=[0, 5, -3] → [[0, 5, -3], [0, -3, 5], [5, 0, -3], [5, -3, 0], [-3, 0, 5], [-3, 5, 0]]

Three distinct numbers give 3! = 6 orderings; the order in which they are listed does not matter.

Example 3
values=[4] → [[4]]
Constraints
  • 1 ≤ length of values ≤ 6
  • -10 ≤ values[i] ≤ 10
  • All numbers in values are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWeight Sets for an Exact Load MediumCard Hands for an Exact Total MediumDistinct Orderings With Repeats MediumEvery Topping Combo MediumWord Traced on a Letter Grid Medium
loading Python…
Input
values=[7, -2]
Expected
[[7, -2], [-2, 7]]

Run checks the first 3 tests in your browser. Submit checks all 48 tests on the server, hidden ones included.