Link to this code:
https://cses.fi/paste/99703477cd4f7d42e1a264/#pragma GCC optimize("O3", "inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define ub upper_bound
const ll INF = 1e18 + 100;
vector<int> solve()
{
int n;
cin >> n;
if(n == 4) return {2, 4, 1, 3};
if(n == 1) return {1};
if(n <= 3) return {-1};
vector<int> ansa(n);
int gps = n / 5;
int ptr = 0;
for(int i = 1; i <= gps * 5; i += 5){
ansa[ptr] = i;
ansa[ptr + 1] = i + 2;
ansa[ptr + 2] = i + 4;
ansa[ptr + 3] = i + 1;
ansa[ptr + 4] = i + 3;
ptr += 5;
}
int rem = n % 5;
if(!rem) return ansa;
int nextNum = gps * 5 + 1;
if(rem == 1) {
ansa[ptr] = n;
return ansa;
}
if(rem == 2) {
ansa[ptr] = ansa[ptr - 1];
ansa[ptr - 1] = nextNum;
ansa[n - 1] = n;
return ansa;
}
if(rem == 3){
ansa[ptr + 1] = ansa[ptr - 1];
ansa[ptr - 1] = nextNum;
ansa[ptr] = nextNum + 2;
ansa[n - 1] = n - 1;
return ansa;
}
if(rem == 4){
ansa[ptr] = nextNum + 1;
ansa[ptr + 1] = nextNum + 3;
ansa[ptr + 2] = nextNum;
ansa[ptr + 3] = nextNum + 2;
return ansa;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--)
{
vector<int> ansa = solve();
if(ansa[0] == -1) cout << "NO SOLUTION\n";
else{
for(int x: ansa) cout << x << " ";
}
}
return 0;
}