You are viewing a single comment's thread. Return to all comments →
function absolutePermutation($n, $k) { $index = []; for($i = 1; $i <= $n; $i++) { $index[$i] = true; } $found = 0; $p = []; for($number = 1; $number <= $n; $number++) { $need1 = $k + $number; $need2 = abs($k - $number); if(abs($need2 - $number) === $k && ($index[$need2] ?? false)) { $index[$need2] = false; $p[] = $need2; $found++; } else if (abs($need1 - $number) === $k && ($index[$need1] ?? false)) { $index[$need1] = false; $p[] = $need1; $found++; } } return $found === $n ? $p : [-1]; }
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 →