how to find all permutations of n distinct integers in c 2b 2b

Solutions on MaxInterview for how to find all permutations of n distinct integers in c 2b 2b by the best coders in the world

showing results for - "how to find all permutations of n distinct integers in c 2b 2b"
Malena
09 Jan 2017
1#include <bits/stdc++.h>
2
3using namespace std;
4
5//Display elements of the array
6void display(vector<int> a, int n){
7    for(int i=0;i<n;i++) cout << a[i] << " ";
8    cout << endl;
9}
10
11int main()
12{
13    //Obtaining length of array
14    int n;
15    cin >> n;
16
17    //Declaring a vector of integers
18    vector<int> a(n);
19    
20    //Taking input of array of integers
21    for(int i=0; i<n; i++){
22        cin >> a[i];
23    }
24
25    do{
26        //Display the current permutation
27        display(a, n);
28    }while(next_permutation(a.begin(), a.end())); //Generate next permutation till it is not lexicographically largest
29
30    return 0;
31}
32C++Copy