-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.cpp
More file actions
55 lines (43 loc) · 1.03 KB
/
Copy pathpermutations.cpp
File metadata and controls
55 lines (43 loc) · 1.03 KB
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
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
std::vector<std::vector<int>> permutations;
void rec(std::vector<int> arr,std::vector<int> p){
if ( arr.size() == 0 )
{
permutations.push_back(p);
return;
}
for (size_t i = 0; i < arr.size(); i++)
{
std::vector<int> arr2 = arr;
std::vector<int> p2 = p;
p2.push_back(arr2[i]);
arr2.erase(arr2.begin()+i);
rec(arr2,p2);
}
}
void call(int i , std::vector<int> arr){
std::vector<int> p = {arr[i]};
arr.erase(arr.begin()+ i);
rec(arr,p);
}
int main(int argc, char const *argv[])
{
std::vector<int> arr = {1,2,3,4};
int s = arr.size();
for (size_t i = 0; i < s; i++)
{
call(i,arr);
}
// printing permutations array
for (size_t i = 0; i < permutations.size(); i++)
{
std::cout<<"[";
for (size_t k = 0; k < s; k++)
{
std::cout<<permutations[i][k]<<",";
}
std::cout<<"], ";
}
std::cout<<"\n";
return 0;
}