Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++ program to display all permutations
- // of an array using STL in C++
- #include <bits/stdc++.h>
- using namespace std;
- int countt{};
- bool check(int a[], int n)
- {
- int l=1;
- for(int i=0;i<n-1;i++){
- while(a[i]<a[i+1]){
- l++;
- i++;
- }
- if(l>=3)
- return false;
- l=1;
- }
- return true;
- }
- // Function to display the array
- void display(int a[], int n)
- {
- for (int i = 0; i < n; i++)
- cout << a[i] << " ";
- cout << endl;
- }
- // Function to find the permutations
- void findPermutations(int a[], int n)
- {
- // Sort the given array
- sort(a, a + n);
- // Find all possible permutations
- cout << "Possible permutations are:\n";
- do {
- if(check(a, n)){
- countt++;
- cout<<"!!!!";
- }
- display(a, n);
- } while (next_permutation(a, a + n));
- }
- // Driver code Ž
- int main()
- {
- int a[] = { 1, 2, 3,4,5,6};
- int n = sizeof(a) / sizeof(a[0]);
- findPermutations(a, n);
- cout<<"final count"<<countt<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement