You are viewing a single comment's thread. Return to all comments →
solution in javascript:
function absolutePermutation(n, k) { const useSet = new Set(); const res = []; for(let i = 1; i<=n; i++){ if((i-k)>=1 && !useSet.has(i-k)){ res.push(i-k); useSet.add(i-k); } else if((i+k) <= n && !useSet.has(i+k)){ res.push(i+k); useSet.add(i+k); } else{ return [-1]; } } return res; }
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 →
solution in javascript: