Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vireshas
Created January 27, 2017 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vireshas/f837741765ddcc5ca90cc749d94ec316 to your computer and use it in GitHub Desktop.
Save vireshas/f837741765ddcc5ca90cc749d94ec316 to your computer and use it in GitHub Desktop.
Absolute Permutation
We define to be a permutation of the first natural numbers in the range . Let denote the position of in permutation (please use -based indexing).
is considered to be an absolute permutation if holds true for every .
Given and , print the lexicographically smallest absolute permutation, ; if no absolute permutation exists, print -1.
Input Format
The first line of input contains a single integer, , denoting the number of test cases.
Each of the subsequent lines contains space-separated integers describing the respective and values for a test case.
Constraints
Output Format
On a new line for each test case, print the lexicographically smallest absolute permutation; if no absolute permutation exists, print -1.
Sample Input
3
2 1
3 0
3 2
Sample Output
2 1
1 2 3
-1
#!/bin/ruby
t = gets.strip.to_i
for a0 in (0..t-1)
n,k = gets.strip.split(' ')
n = n.to_i
k = k.to_i
if (n % 2 != 0 and k != 0)
puts "-1"
elsif k == 0
1.upto(n).each {|i|
print i
print " "
}
puts
else
ar = []
c = 0
1.upto(n).each {|i|
if !ar[i]
c += 2
ar[i] = i + k
ar[i + k] = i
end
}
if c != n
puts "-1"
else
puts ar[1..-1].join(" ")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment