mediumBacktrackingPure DSA~23 min
Experiment Order Permutations
An A/B platform must counterbalance the order in which test arms are shown to remove ordering bias. Given the distinct arm ids, generate every possible ordering so each can be assigned to a cohort.
Problem
Given an array of distinct integers, return all possible orderings (permutations) of the elements. Any order of the permutations is accepted.
Input
An array nums of length n (1 ≤ n ≤ 8) of distinct integers.
Output
A list of all n! permutations, each a list of integers.
Constraints
- 1 ≤ n ≤ 8
- All elements are distinct
- Output contains exactly n! permutations
Examples
Example 1
Input
nums = [1, 2, 3]
Output
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
All 3! = 6 orderings of three distinct arms.
Example 2
Input
nums = [1]
Output
[[1]]
A single element has one ordering.