 Simple-Easy

Recursion

# PROBLEM:

Deepesh’s mobile got locked. He forgot his passcode but he is aware of digits used in his passcode.
Generate and print all the different passcode possible from the given known digits.
There will not be any repeated digit.

# EXPLANATION:

Generate it using permutations by recursion. use a recursive function with proper base condition to display the value.

# SOLUTIONS:

Setter's Solution
`````` #include <iostream>
using namespace std;

// Function to find all permutations of a given string `str[i…n-1]`
// containing all distinct characters
void permutations(string str, int i, int n)
{
// base condition
if (i == n - 1)
{
cout << str << endl;
return;
}

// process each character of the remaining string
for (int j = i; j < n; j++)
{
// swap character at index `i` with the current character
swap(str[i], str[j]);        // STL `swap()` used

// recur for substring `str[i+1, n-1]`
permutations(str, i + 1, n);

// backtrack (restore the string to its original state)
swap(str[i], str[j]);
}
}

int main()
{
int n;
cin>>n;
string str="";
for(int i=0;i<n;i++){
int k;
cin>>k;
str+=(to_string(k));
}

permutations(str, 0, str.length());

return 0;}
``````