Given the following code snippet :
string s = “bca”;
do {
cout << s << ’ ';
} while(next_permutation(s.begin(), s.end()));
cout << s;
What is the output of the given code?
bca cab cba abc
bca cab cba cba
bca bca bca bca
bca cab cba bca
Given the following code snippet :
string s = “bca”;
do {
cout << s << ’ ';
} while(next_permutation(s.begin(), s.end()));
cout << s;
What is the output of the given code?
bca cab cba abc
bca cab cba cba
bca bca bca bca
bca cab cba bca
All permutations of abc arranged in lexicographical order are
abc acb bac bca cab cba
Since next permutation generates the string next in lexicographical order we can see that cab comes after bca and cba comes after cab. Since cba is largest permutation after that it’ll generate the smallest one i.e abc
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.