https://www.hackerrank.com/challenges/absolute-permutation/problem
I know that if you work out the math and logic first, the algorithm can be simplified greatly (shifting indices), but is that the only way to solve this and pass all test cases? Checking if a value is in a list can be slow, but I don't know how to optimize/change that to a better code. Without altering the whole logic of my code, is it possible to tweak it to pass all test cases?
def absolutePermutation(number, k):
newlist=[]
for i in range(1,number+1):
bigger=k+i
smaller=i-k
tmp=set(newlist)
if (bigger>number and smaller<=0) or (bigger>number and smaller in tmp) or (smaller<=0 and bigger in tmp):
return [-1]
if smaller<=0 or smaller in tmp:
newn=bigger
else:
newn=smaller
#print(newn)
newlist.append(newn)
return newlist