• + 0 comments

    Here is my code which pass all tests, but I saw a lot of check about n%(2*k)==0 and don't really understand why and I'm curious if someone would care to explain

    public static List<Integer> absolutePermutation(int n, int k) {
    // Write your code here
        Map<Integer,Integer> map = new TreeMap<>();
        for(int i=1;i<=n;i++){
            if(i-k>0&&!map.containsKey(i-k)){
                map.put(i-k,i);
            }else if(i+k<=n){
                map.put(i+k,i);
            } else{
                map = new TreeMap<>();
                map.put(1,-1);
                break;
            }
        }
        return new ArrayList<>(map.values());
    }