-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.java
146 lines (123 loc) · 4.39 KB
/
Solution.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//Problem: https://www.hackerrank.com/challenges/absolute-permutation
//Java 8
/*
Find every perumation
Do a linear traversal of each perm checking if condition holds
we know that the only permutations we care about are ones that satistfy
absolute
for example
1 2 3 4 5 if k is 1 we know for every point we can only have a number 1 greater or 1 less
_ 1 2 3 4
2 3 4 5 _
so any combinations of those values at those points
the underscore means we only have 1 choice at those points
Example:
10 2
_ _ _ _ _ _ _ _ _ _
3 _ _ _ _ _ _ _ _ 8
3 4 _ _ _ _ _ _ 7 8
3 4 1 _ _ _ _ 10 7 8
3 4 1 2 _ _ 9 10 7 8
3 4 1 2 _ _ 9 10 7 8 neither is available so failed
so it appears we can solve this in O(n) time by
working from outside in until we hit an impossibility
we want to loop n/2 times and each time check the left most
untouched index and the right most untouched index
we will also need to track what numbers we have already used
left most
check if index - k is in bounds if it is check if it is used if not use it
else if check if i + k is in bounds and is used if it is and is not use it
else not possible
right most
check if index plus k is in bounds if it is check it it is used if not use it
else check if i - k is in bounds and is used if it is and is not use it
else not possible
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
tests :
for(int a0 = 0; a0 < t; a0++){
int n = in.nextInt();
int k = in.nextInt();
int[] array = new int[n];
Set<Integer> used = new HashSet<>();
//Iterate from left and right filling in the array according to the algorithm above
for(int i = 0; i < n/2; i++)
{
int leftMost = i + 1;
int rightMost = n - i;
//Left most
if(leftMost - k > 0 && !used.contains(leftMost-k))
{
array[i] = leftMost-k;
used.add(leftMost-k);
}
else if(i + 1 + k <= n && !used.contains(leftMost+k))
{
array[i] = leftMost+k;
used.add(leftMost+k);
}
else
{
System.out.println("-1");
continue tests;
}
//Right most
if(rightMost + k <= n && !used.contains(rightMost+k))
{
array[n-1-i] = rightMost+k;
used.add(rightMost+k);
}
else if(rightMost - k > 0 && !used.contains(rightMost-k))
{
array[n-1-i] = rightMost-k;
used.add(rightMost-k);
}
else
{
System.out.println("-1");
continue tests;
}
}
//if it is odd check to see if we have a number for the middle index
if(n % 2 == 1)
{
int middle = (n/2) + 1;
if(!used.contains(middle+k) || !used.contains(middle-k))
{
if(!used.contains((n/2) +1 + k) && middle + k <= n)
{
array[(n/2)] = (n/2) + 1 + k;
}
else if(!used.contains((n/2) +1 - k) && middle - k > 0)
{
array[(n/2)] = (n/2) + 1 - k;
}
else
{
System.out.println("-1");
continue tests;
}
}
else
{
System.out.println("-1");
continue tests;
}
}
//Print the results of a success
for(int num : array)
{
System.out.print(num+" ");
}
System.out.println("");
}
}
}