Last active
December 14, 2017 12:19
-
-
Save carousel/8c6840b5d53ac71b95caa17910174c9f to your computer and use it in GitHub Desktop.
Binary gap algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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