JOIN
 Revision History
 Select a Forum     Round Tables New Member Discussions News Discussions Algorithm Matches Marathon Matches NASA Tournament Lab TopCoder Cookbook High School Matches Sponsor Discussions Development Forums Design Forums Search My Post History  |  My Watches  |  User Settings Forums TopCoder Cookbook Algorithm Competitions - Rewriting Phase Iterating Over All Permutations of an Array Revision History (12 edits)
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; class Permutations { public: int howMany (vector a) { sort(a.begin(),a.end()); int ans=0; do { ++ans; } while (next_permutation(a.begin(),a.end())); return ans; } }; ```Done it, we can enjoy the life. But this is still a problem for coders who aren’t experiencing C++ language and thus have no such a great tool as STL. While sorting arrays is very common procedure for which we refer to other recipes, generating next permutation is not as well. So we should show how to write such a function manually. This code represents Java function bool my_next_permutation (int []), whose arguments are an array and number of permutating elements in it, and this function’s action copies C++ STL function’s one:```public boolean my_next_permutation(int[] a) { int n=a.length; int i,j,k,temp; i=n-2; while (i>=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; class Permutations { public: int howMany (vector a) { sort(a.begin(),a.end()); int ans=0; do { ++ans; } while (next_permutation(a.begin(),a.end())); return ans; } }; ```Done it, we can enjoy the life. But this is still a problem for coders who aren’t experiencing C++ language and thus have no such a great tool as STL. While sorting arrays is very common procedure for which we refer to other recipes, generating next permutation is not as well. So we should show how to write such a function manually. This code represents Java function bool my_next_permutation (int []), whose arguments are an array and number of permutating elements in it, and this function’s action copies C++ STL function’s one:```public boolean my_next_permutation(int[] a) { int n=a.length; int i,j,k,temp; i=n-2; while (i>=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j= comparing array elements turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j #include #include using namespace std; class TheLuckyString { public: int count (string a) { int i; int ans=0; sort(a.begin(),a.end()); reverse(a.begin(),a.end()); do { for (i=0; i
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; class Permutations { public: int howMany (vector a) { sort(a.begin(),a.end()); int ans=0; do { ++ans; } while (next_permutation(a.begin(),a.end())); return ans; } }; ```Done it, we can enjoy the life. But this is still a problem for coders who aren’t experiencing C++ language and thus have no such a great tool as STL. While sorting arrays is very common procedure for which we refer to other recipes, generating next permutation is not as well. So we should show how to write such a function manually. This code represents Java function bool my_next_permutation (int []), whose arguments are an array and number of permutating elements in it, and this function’s action copies C++ STL function’s one:```public boolean my_next_permutation(int[] a) { int n=a.length; int i,j,k,temp; i=n-2; while (i>=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j= comparing array elements turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j #include #include using namespace std; class TheLuckyString { public: int count (string a) { int i; int ans=0; sort(a.begin(),a.end()); reverse(a.begin(),a.end()); do { for (i=0; i
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; class Permutations { public: int howMany (vector a) { sort(a.begin(),a.end()); int ans=0; do { ++ans; } while (next_permutation(a.begin(),a.end())); return ans; } }; ```Done it, we can enjoy the life. But this is still a problem for coders who aren’t experiencing C++ language and thus have no such a great tool as STL. While sorting arrays is very common procedure for which we refer to other recipes, generating next permutation is not as well. So we should show how to write such a function manually. This code represents Java function bool my_next_permutation (int []), whose arguments are an array and number of permutating elements in it, and this function’s action copies C++ STL function’s one:```public boolean my_next_permutation(int[] a) { int n=a.length; int i,j,k,temp; i=n-2; while (i>=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j= comparing array elements turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j #include #include using namespace std; class TheLuckyString { public: int count (string a) { int i; int ans=0; sort(a.begin(),a.end()); reverse(a.begin(),a.end()); do { for (i=0; i
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; class Permutations { public: int howMany (vector a) { sort(a.begin(),a.end()); int ans=0; do { ++ans; } while (next_permutation(a.begin(),a.end())); } }; ```Done it, we can enjoy the life. But this is still a problem for coders who aren’t experiencing C++ language and thus have no such a great tool as STL. While sorting arrays is very common procedure for which we refer to other recipes, generating next permutation is not as well. So we should show how to write such a function manually. This code represents Java function bool my_next_permutation (int []), whose arguments are an array and number of permutating elements in it, and this function’s action copies C++ STL function’s one:```public boolean my_next_permutation(int[] a) { int n=a.length; int i,j,k,temp; i=n-2; while (i>=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j= comparing array elements turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j #include #include using namespace std; class TheLuckyString { public: int count (string a) { int i; int ans=0; sort(a.begin(),a.end()); reverse(a.begin(),a.end()); do { for (i=0; i
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; const int c=100; int n; int a[c]; int main() { scanf("%d",&n); int i; for (i=0; i=0 && a[i]>=a[i+1]) --i; if (i<0) { reverse(a,a+n); return 0; } j=n-1; while (a[i]>=a[j]) --j; swap(a[i],a[j]); reverse(a+i+1,a+n); return 1; } ```DiscussionUsing these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with operators >= comparing array elements turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; const int c=100; int n; int a[c]; int main() { scanf("%d",&n); int i; for (i=0; i=0 && a[i]>=a[i+1]) --i; if (i<0) { reverse(a,a+n); return 0; } j=n-1; while (a[i]>=a[j]) --j; swap(a[i],a[j]); reverse(a+i+1,a+n); return 1; } ```DiscussionUsing these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with all operators >= turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; const int c=100; int n; int a[c]; int main() { scanf("%d",&n); int i; for (i=0; i=0 && a[i]>=a[i+1]) --i; if (i<0) { reverse(a,a+n); return 0; } j=n-1; while (a[i]>=a[j]) --j; swap(a[i],a[j]); reverse(a+i+1,a+n); return 1; } ```DiscussionA picture below may help to understand how next permutation is generated from the current one:Using these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with all operators >= turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; const int c=100; int n; int a[c]; int main() { scanf("%d",&n); int i; for (i=0; i=0 && a[i]>=a[i+1]) --i; if (i<0) { reverse(a,a+n); return 0; } j=n-1; while (a[i]>=a[j]) --j; swap(a[i],a[j]); reverse(a+i+1,a+n); return 1; } ```DiscussionA picture below may help to understand how next permutation is generated from the current one:Using these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with all operators >= turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; const int c=100; int n; int a[c]; int main() { scanf("%d",&n); int i; for (i=0; i=0 && a[i]>=a[i+1]) --i; if (i<0) { reverse(a,a+n); return 0; } j=n-1; while (a[i]>=a[j]) --j; swap(a[i],a[j]); reverse(a+i+1,a+n); return 1; } ```DiscussionUsing these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with all operators >= turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; const int c=100; int n; int a[c]; int main() { scanf("%d",&n); int i; for (i=0; i=0 && a[i]>=a[i+1]) --i; if (i<0) { reverse(a,a+n); return 0; } j=n-1; while (a[i]>=a[j]) --j; swap(a[i],a[j]); reverse(a+i+1,a+n); return 1; } ```DiscussionUsing these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with all operators >= turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; const int c=100; int n; int a[c]; int main() { scanf("%d",&n); int i; for (i=0; i=0 && a[i]>=a[i+1]) --i; if (i<0) { reverse(a,a+n); return 0; } j=n-1; while (a[i]>=a[j]) --j; swap(a[i],a[j]); reverse(a+i+1,a+n); return 1; } ```DiscussionUsing these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with all operators >= turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and i (with minimal possible aj under these constraints), then swap ai and aj and reorder all elements with indices more than i non-decreasing.And our my_next_permutation does exactly this. First of all, we go from the last element to find the first such element that has greater right neighbor – and this is ai. We see that the sequence of elements with indices greater than i is ordered non-increasing. Then we find the minimal element with index j>i such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j, of course), new sequence of elements with indices greater than i is still ordered non-increasing, so we should just reverse it to obtain non-decreasing sequence.Another question may be: What is the time complexity of algorithm? Answer is T(n)=A(n)*B(n), where T(n) is time complexity of whole algorithm of iterating all permutations, A(n) is an estimate of number of permutations, and B(n) is time complexity of getting next permutation. Of course, if we have an array with all elements distinct, A(n)=n!, and if there are k distinct groups of equal elements with size Si of i-th group, an answer should be n!/(S1!*…*Si!*…*Sk!). And B(n)=O(n), as we can see easily, because there are only two non-nested cycles in function my_next_permutation, and each of them run along the some part of array of length n. So if there are no equal elements in the array, time complexity of whole algorithm is O(n!*n).As for the problem example, you can look at TheLuckyString.
 Iterating Over All Permutations of an Array NameIterating Over All Permutations of an ArrayProblemYou have an array of objects supporting comparison, and you want to iterate through all possible permutations of these objects.SolutionAssuming that quite natural order of permutations is lexicographic order, you should be able to do just three things:1) Generate the first permutation;2) Get the next permutation from a given one;3) Recognize the last permutation.And then you can use this simple algorithm (in pseudo-code):```array a[n]; generate_first_permutation(a); while (1) { // Do something with the array. if (is_last_permutation(a)) break; get_next_permutation(a); } ```Of course, as soon as the first permutation is just a lexicographically first, we can use for generating it well-known quick sort algorithm. Even more, in C++ STL there is nice function bool next_permutation (BidirectionalIterator first, BidirectionalIterator last) (and rather like this, the function named prev_permutation, if you want to iterate in reverse order). If the given permutation is not last, this function generates the next permutation and returns 1, else it generates the first permutation and returns 0. (Note that this function works correctly even if there are equal elements in the array.) Using all these, we can easily write our program in C++:```#include #include using namespace std; const int c=100; int n; int a[c]; int main() { scanf("%d",&n); int i; for (i=0; i=0 && a[i]>=a[i+1]) --i; if (i<0) { reverse(a,a+n); return 0; } j=n-1; while (a[i]>=a[j]) --j; swap(a[i],a[j]); reverse(a+i+1,a+n); return 1; } ```DiscussionUsing these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with all operators >= turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j