-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPermutation.cpp
More file actions
45 lines (37 loc) · 788 Bytes
/
Permutation.cpp
File metadata and controls
45 lines (37 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
vector<vector<int> > ans;
// void permute(vector<int> &arr,int idx){
void permute(vector<int> arr,int idx){
if(idx==arr.size()){
ans.push_back(arr);
return;
}
for(int i=idx;i<arr.size();i++){
if(i!=idx && arr[i]==arr[idx]){
continue;
}
swap(arr[i],arr[idx]);
permute(arr,idx+1);
}
return;
}
int main(){
int n;
cin>>n;
vector<int> arr(n);
for(auto &i : arr){
cin>>i;
}
// Way-1
permute(arr,0);
// Way-2
// do{
// ans.push_back(arr);
// }while(next_permutation(arr.begin(),arr.end()));
for(auto a : ans){
for(auto b : a){
cout<<b<<" ";
}cout<<endl;
}
}