Permutations using backtracking

PHOTO EMBED

Thu Jun 08 2023 02:58:31 GMT+0000 (Coordinated Universal Time)

Saved by @DxBros #c++ #permutations #backtracking

    void print(vector<int>& v){
        for(auto ele : v)
            cout << ele << " ";
        cout<< endl;
    }
    void backtrack(int index, vector<int> ans){
        if(index == (ans.size() - 1)){
            print(ans);
            return;
        }
        for(int i = index; i < ans.size(); i++){
            swap(ans[index], ans[i]);
            backtrack(index+1, ans);
            swap(ans[index], ans[i]);
        }
    }
content_copyCOPY

https://practice.geeksforgeeks.org/problems/find-kth-permutation-0932/1