Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. // C++ program to display all permutations
  2. // of an array using STL in C++
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. int countt{};
  7. bool check(int a[], int n)
  8. {
  9.     int l=1;
  10.     for(int i=0;i<n-1;i++){
  11.         while(a[i]<a[i+1]){
  12.             l++;
  13.             i++;
  14.         }
  15.         if(l>=3)
  16.             return false;
  17.         l=1;
  18.     }
  19.     return true;
  20. }
  21. // Function to display the array
  22. void display(int a[], int n)
  23. {
  24.     for (int i = 0; i < n; i++)
  25.         cout << a[i] << " ";
  26.     cout << endl;
  27. }
  28.  
  29. // Function to find the permutations
  30. void findPermutations(int a[], int n)
  31. {
  32.  
  33.     // Sort the given array
  34.     sort(a, a + n);
  35.  
  36.     // Find all possible permutations
  37.     cout << "Possible permutations are:\n";
  38.     do {
  39.         if(check(a, n)){
  40.             countt++;
  41.             cout<<"!!!!";
  42.         }
  43.         display(a, n);
  44.     } while (next_permutation(a, a + n));
  45. }
  46.  
  47. // Driver code Ž
  48. int main()
  49. {
  50.  
  51.     int a[] = { 1, 2, 3,4,5,6};
  52.  
  53.     int n = sizeof(a) / sizeof(a[0]);
  54.  
  55.     findPermutations(a, n);
  56.     cout<<"final count"<<countt<<endl;
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement