Skip to content

Instantly share code, notes, and snippets.

@MagedAhmad
Created April 19, 2020 16:23
Show Gist options
  • Select an option

  • Save MagedAhmad/a2fd024b5de5e9c022c60a33e3dea392 to your computer and use it in GitHub Desktop.

Select an option

Save MagedAhmad/a2fd024b5de5e9c022c60a33e3dea392 to your computer and use it in GitHub Desktop.
Hackerrank minimum Swaps
<?php
function minimumSwaps($arr) {
$swaps = 0;
for($i = 0; $i < count($arr); $i++) {
while($arr[$i] != $i + 1) {
$temp = $arr[$i];
$arr[$i] = $arr[$temp-1];
$arr[$temp-1] = $temp;
$swaps++;
}
}
return $swaps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment