• + 0 comments

    This a is bit more intuitive approach.

    def condition(i: int, k: int, n: int, a: set) -> bool:
        return 0 < i + k <= n and (i + k) not in a
    
    
    def absolutePermutation(n: int, k: int) -> int | list[int]:
        ans = [-1 for _ in range(n + 1)]
        a = set()
        for i in range(1, n + 1):
            if condition(i, -k, n, a) is True:
                ans[i] = i - k
                a.add(i - k)
            elif condition(i, k, n, a) is True:
                ans[i] = i + k
                a.add(i + k)
            else:
                return [-1]
        return ans[1:]