We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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());
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Absolute Permutation
You are viewing a single comment's thread. Return to all 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