Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Last active March 29, 2023 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/8994e1810cb06b60c40c597e8a195fa8 to your computer and use it in GitHub Desktop.
Save danielhaim1/8994e1810cb06b60c40c597e8a195fa8 to your computer and use it in GitHub Desktop.
Inverse Permutation Algorithm
function inversePermutation(permutation) {
const n = permutation.length;
const result = Array(n);
for (let i = 0; i < n; i++) {
result[permutation[i] - 1] = i + 1;
}
return result;
}
// const permutation = [3, 1, 4, 5, 2];
// const inverse = inversePermutation(permutation);
// console.log(inverse); // [2, 1, 5, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment