Skip to content

Instantly share code, notes, and snippets.

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 gadelkareem/d3b2282f363f4b9a2475b75e989a452a to your computer and use it in GitHub Desktop.
Save gadelkareem/d3b2282f363f4b9a2475b75e989a452a to your computer and use it in GitHub Desktop.
Greatest Sibling Number
<?php
function solution(int $n) {
$final = [];
$nArr = str_split($n);
$i = 0;
while (true) {
$el = pickGreatest($nArr);
$final[$i] = $nArr[$el];
unset($nArr[$el]);
$nArr = array_values($nArr);
if (count($nArr) == 0) {
break;
}
$i++;
}
return (int)implode($final, "");
}
function pickGreatest(array $nArr)
{
$x = -1;
$el = -1;
for ($i = 0; $i < count($nArr); $i++) {
if ($nArr[$i] > $x) {
$x = $nArr[$i];
$el = $i;
}
}
return $el;
}
solution(8760328194);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment