> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/46-permutations.md).

# 46 Permutations

Given a collection of**distinct**numbers, return all possible permutations.

For example,\
`[1,2,3]`have the following permutations:

```
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
```

**The Idea:** Recursively append the `i+1th` element to every position for every permutation in the previous set of permutations until the end is reached.

**Complexity:** O(n! \* n)

```cpp
vector<vector<int>> _permute(vector<vector<int>> &prev, const vector<int> &orig, int index) {
    if (index >= orig.size()) return prev;

    vector<vector<int>> next; 
    next.reserve((prev[0].size() + 1) * prev.size());
    for (auto perm : prev) {
        vector<int> perm_cp = perm;
        for (int i = 0; i <= perm.size(); i++) {
            int add = orig.at(index);
            perm_cp.insert(perm_cp.begin() + i, add);
            next.push_back(perm_cp);
            perm_cp = perm;
        }
    }

    return _permute(next, orig, index + 1);
}

vector<vector<int>> permute(vector<int>& nums) {
    vector<vector<int>> permutations;
    if (nums.empty()) return permutations;

    permutations.push_back(vector<int>(1,nums[0]));
    return _permute(permutations, nums, 1);
}
```

**Python**

```python
class Solution:
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        sol = [[]]
        for n in nums:
            next_sol = []
            for prev in sol:
                # ab, ba
                for i in range(0, len(prev) + 1):
                    # cab, acb, abc
                    next_sol.append(prev[:i] + [n] + prev[i:])
            sol = next_sol
        return sol
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/46-permutations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
