You are viewing a single comment's thread. Return to all comments →
js
function absolutePermutation(n, k) { let a = []; for (let i = 1; i <= n; i++) { a.push(i); } k = Math.abs(k); if (k == 0) return a; else if (k > n / 2 + 1 || n % k != 0 || (n/k % 2 != 0)) return [-1]; else { a = []; for (let i = 1; i <= n; i++) { a.push(i + Math.pow(-1, Math.floor((i - 0.1) / k)) * k); } return a; } }
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
js