CodePark/ problems / Every Ordering
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Backtracking

Every Ordering

mediumabout 20 minutes in an interview

nums holds distinct integers. Return every way to line them all up. Each line-up uses every number once. A different order is a different line-up, so [4, 6] and [6, 4] are both in the answer.

  • Return an array of arrays. Each inner array is as long as nums.
  • List each line-up once only.
  • The line-ups can come in any order in the result. But the order of numbers inside a line-up is what makes it that line-up, so that part matters.
  • nums has 0 to 7 numbers, each between -10 and 10. If it is empty, return [[]], because there is exactly one way to line up nothing.
  • Don't change nums.
Input: nums = [4, 6, 8] Output: [[4, 6, 8], [4, 8, 6], [6, 4, 8], [6, 8, 4], [8, 4, 6], [8, 6, 4]]There are 3 choices for the first spot, 2 for the second and 1 for the last. So there are 3 × 2 × 1 = 6 orderings.
Input: nums = [5] Output: [[5]]