I was solving the CSES problem Permutations. In the questions, the user is asked to input the internet n of constraints 1≤n≤10^6 and should construct a beautiful permutation if such a permutation exists. A permutation of integers 1,2,…,n is called beautiful if there are no adjacent elements whose difference is 1.
Input
The only input line contains an integer n.
Output
Print a beautiful permutation of integers 1,2,…,n. If there are several solutions, you may print any of them. If there are no solutions, print "NO SOLUTION".
Constraints 1≤n≤10^6
Example 1
Input:
5
Output:
4 2 5 3 1
Example 2
Input:
3
Output:
NO SOLUTION
I could pass all the test cases except the last one which says "TIME EXCEED LIMIT " for the input 1000000.
Here is the code for my algorithm:
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long int n;
cin>>n;
if(n>3 || n<2)
{
for(long long int i=2;i<=n;i+=2)
cout<<i<<endl;
for(long long int j=1;j<=n;j+=2)
cout<<j<<endl;
}
else{
cout <<"NO SOLUTION";
}
return 0;
}