-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
66 lines (62 loc) · 917 Bytes
/
Permutations.cpp
File metadata and controls
66 lines (62 loc) · 917 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
int give_pos(vector<bool> &used,int &position)
{
int n = used.size()-1;
for(int i=1;i<=n;i++)
{
if(abs(i-position)<=1)
continue;
if(used[i])
continue;
else
{
used[i] = true;
return i;
}
}
return -1;
}
void solve(int &n)
{
if(n==1)
{
cout<<1<<endl;
return;
}
if(n==2 || n==3)
{
cout<<"NO SOLUTION"<<endl;
return;
}
vector<bool> used(n+1,false);
used[0]=true;
int check = 1;
int pos = n-1;
used[n-1]=true;
vector<int> ans;
while(check<=n)
{
ans.push_back(pos);
pos = give_pos(used,pos);
check++;
}
for(int &x:ans)
{
cout<<x<<" ";
}
return;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin>>n;
solve(n);
return 0;
}