-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day13.java
38 lines (34 loc) · 1008 Bytes
/
Day13.java
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
import java.util.*;
public class Day13 {
public static List<Integer> absolutePermutation(int n, int k) {
List<Integer> permutation = new ArrayList<>();
// Check for special cases
if (k == 0) {
// If k is 0, return the numbers from 1 to n in ascending order
for (int i = 1; i <= n; i++) {
permutation.add(i);
}
} else if (n % (2 * k) == 0) {
// If n is divisible by 2k
for (int i = 1; i <= n; i++) {
int target = (i - 1) / k;
if (target % 2 == 0) {
permutation.add(i + k);
} else {
permutation.add(i - k);
}
}
} else {
permutation.add(-1); // No valid permutation exists
}
return permutation;
}
public static void main(String[] args) {
int n=3;
int k=0;
List<Integer> result= absolutePermutation(n,k);
for(int i=0; i<result.size(); i++) {
System.out.println(result.get(i));
}
}
}