Skip to content

Instantly share code, notes, and snippets.

@carousel
Last active December 14, 2017 12:19
Show Gist options
  • Save carousel/8c6840b5d53ac71b95caa17910174c9f to your computer and use it in GitHub Desktop.
Save carousel/8c6840b5d53ac71b95caa17910174c9f to your computer and use it in GitHub Desktop.
Binary gap algorithm
<?php
function binaryGap($n)
{
$result = [];
$tab = explode("1", decbin($n));
if ($tab[count($tab) - 1] != "") {
array_pop($tab);
}
foreach ($tab as $t) {
if ($t != "") {
$result[] = $t;
}
}
if (!empty($result)) {
return $maxlen = max(array_map('strlen', $result));
} else {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment