Skip to content

Commit ea0b552

Browse files
Merge pull request #454 from agarwalsahil0210/master
Added permutations
2 parents f8f4c33 + 088785c commit ea0b552

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//A permutation of integers 1,2,…,n is called beautiful if there are no adjacent elements whose difference is 1. Given n, construct a beautiful permutation if such a permutation exists.
2+
//Author : agarwalsahil0210
3+
//User id: https://cses.fi/user/32363
4+
//Ques. link : https://cses.fi/problemset/task/1070/
5+
6+
7+
import java.util.Scanner;
8+
public class permutation
9+
{
10+
public static void main(String[] args)
11+
{
12+
Scanner sc=new Scanner(System.in);
13+
int x=sc.nextInt();
14+
int[] arr = new int[x];
15+
if(x==1)
16+
{
17+
System.out.print("1");
18+
System.exit(0);
19+
}
20+
if(x==2 || x==3)
21+
{
22+
System.out.print("NO SOLUTION");
23+
System.exit(0);
24+
}
25+
if(x==4)
26+
{
27+
System.out.println("2 4 1 3");
28+
System.exit(0);
29+
}
30+
for(int i=1; i<=x; i++)
31+
{
32+
arr[i-1]=i;
33+
}
34+
for(int i=0; i<x; i+=2)
35+
{
36+
System.out.print(arr[i]+" ");
37+
}
38+
for(int i=1; i<x; i+=2)
39+
{
40+
System.out.print(arr[i]+" ");
41+
}
42+
sc.close();
43+
}
44+
}

0 commit comments

Comments
 (0)