-
Notifications
You must be signed in to change notification settings - Fork 0
/
Good_Permutation.cpp
47 lines (41 loc) · 965 Bytes
/
Good_Permutation.cpp
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
/*
GOOD PERMUTATION
A good permutation of a natural number N, is an arrangement
of first N natural numbers such that,
No number is present at it's position ( A[i] != i)
and
The value of a position is the index where the position is
present as a value. ( A[A[i]] = i )
When such a permutation is not present, we print -1.
When we look at the second requirement, we find that if we
swap the consecutive numbers, we are able to satisfy the
requirements. When N is odd, the swaps can not be possible.
Hence, we print -1.
*/
#include<bits/stdc++.h>
using namespace std;
void permute(int n) {
if (n % 2) {
cout<<-1;
}
else {
for (int i = 0; i < n; i++) {
if (i % 2) {
cout<<i<<" ";
}
else {
cout<<i + 2<<" ";
}
}
}
}
int main() {
cout<<"Enter N: ";
int n;
cin>>n;
permute(n);
}
/*
INPUT : n = 6
OUTPUT : 2 1 4 3 6 5
*/