Skip to content

Instantly share code, notes, and snippets.

@habibtalib
Forked from NEbere/binarygap.php
Created May 29, 2017 05:48
Show Gist options
  • Save habibtalib/7b5e008a01baf71f9b488cbeaaadbfac to your computer and use it in GitHub Desktop.
Save habibtalib/7b5e008a01baf71f9b488cbeaaadbfac to your computer and use it in GitHub Desktop.
// Codility Bbinary Gap Test
function solution($N) {
// write your code in PHP5.5
$binaryNumber = decbin($N); // binary conversion of number
$trimmed = trim($binaryNumber, 0); // trim to remove trailing zeros
$binaryGap = explode("1",$trimmed); // explode
$binaryCount = array_map('strlen', $binaryGap);
return max($binaryCount); // returns the longest binary gap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment