<p>Given two integers <code>n</code> and <code>k</code>, return <em>all possible combinations of</em> <code>k</code> <em>numbers chosen from the range</em> <code>[1, n]</code>.</p>

<p>You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 4, k = 2
<strong>Output:</strong> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
<strong>Explanation:</strong> There are 4 choose 2 = 6 total combinations.
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1, k = 1
<strong>Output:</strong> [[1]]
<strong>Explanation:</strong> There is 1 choose 1 = 1 total combination.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 20</code></li>
	<li><code>1 &lt;= k &lt;= n</code></li>
</ul>

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        vector<int> arr;
        set<vector<int>> s;
        vector<int> ds;
        map<int, bool> mpp;
        vector<vector<int>> res;
        
        for (int i = 1; i <= n; i++) {
            arr.push_back(i);  // Fill the array with numbers from 1 to n
        }
        for (int i = 0; i < arr.size(); i++) {
            mpp[arr[i]] = false;  // Initialize the map with false values
        }
        
        comb(0, arr, s, ds, res, mpp, k);
        return res;
    }
    
    void comb(int ind, vector<int> arr, set<vector<int>>& s, vector<int>& ds, vector<vector<int>>& res, map<int, bool>& mpp, int k) {
        if (ds.size() == k) {
            if (s.find(ds) == s.end()) {
                res.push_back(ds);
                s.insert(ds);
            }
            return;
        }
        
        for (int i = ind; i < arr.size(); i++) {  // Use i instead of ind to iterate over the array
            if (mpp[arr[i]] == false) {  // Check the map for arr[i]
                mpp[arr[i]] = true;
                ds.push_back(arr[i]);
                comb(i + 1, arr, s, ds, res, mpp, k);  // Use i + 1 to move forward
                ds.pop_back();
                mpp[arr[i]] = false;
            }
        }
    }
};

182987