# 46. Permutations

## Problem Statement

<br>

Given an array `nums` of distinct integers, return *all the possible permutations*. You can return the answer in **any order**.

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [1,2,3]
</strong><strong>Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [0,1]
</strong><strong>Output: [[0,1],[1,0]]
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: nums = [1]
</strong><strong>Output: [[1]]
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= nums.length <= 6`
* `-10 <= nums[i] <= 10`
* All the integers of `nums` are **unique**.<br>

<br>

## Intuition

```
Take an element at every position

Approach 1 :
MAintain a map for marking which element is taken and push the elements accordingly

Approach 2: Eliminate the extra map space

Basically, Swap elemnts such that all elemets gets a chance at all position

Once swapped, swap other elements of remaining array
```

### Links

<https://leetcode.com/problems/permutations/description/>

### Video Links

<https://www.youtube.com/watch?v=YK78FU5Ffjw&ab_channel=takeUforward>\
\
<https://www.youtube.com/watch?v=f2ic2Rsc9pU&ab_channel=takeUforward>

### Approach 1:

```
Taking map
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    vector<vector<int>> find_ans(vector<vector<int>> &ans, vector<int> &nums, 
            vector<int> &temp, unordered_map<int, bool> &mp){

        if(temp.size() == nums.size()){
            ans.push_back(temp);
            return ans;
        }
        
        /*
            Pushing and marking the element in the map
            When we return unmark the array and proceed
        */

        for(int i=0; i<nums.size(); i++){
            if(mp.find(i) == mp.end()){
                temp.push_back(nums[i]);
                mp[i] = true;

                find_ans(ans, nums, temp, mp);

                temp.pop_back();
                mp.erase(i);
            }
        }
        
        return ans;
    }

    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> ans;
        unordered_map<int, bool> mp;
        vector<int> temp;
        
        return find_ans(ans, nums, temp, mp);
    }
};
```

{% endcode %}

### Approach 2:

```
Elimination of map
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    vector<vector<int>> find_ans(vector<vector<int>> &ans, vector<int> &nums, int index){
        if(index == nums.size()){
            ans.push_back(nums);
            return ans;
        }

        for(int i=index; i<nums.size(); i++){
            swap(nums[index], nums[i]);
            find_ans(ans, nums, index+1);
            swap(nums[index], nums[i]);
        }

        return ans;
    }

    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> ans;
        return find_ans(ans, nums,  0);
    }
};
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: 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:

```
GET https://coding-9.gitbook.io/untitled/recursion-and-backtracking/46.-permutations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
