-
FundamentalsLogic
Code // > given a positive integer N // do not have to account for N of 0 function binaryGap($n) { return preg_match_all('/1(0+)1/', decbin($n), $matches) ? max(array_map('strlen', $matches[1])) : 0; }
Test Cases <?php use PHPUnit\Framework\TestCase; // PHPUnit Test Examples: // TODO: Replace examples and use TDD by writing your own tests class ExampleTest extends TestCase { // test function names should start with "test" public function testThatSomethingShouldHappen() { $this->assertEquals(5, binaryGap(1041)); $this->assertEquals(2, binaryGap(9)); $this->assertEquals(0, binaryGap(15)); } }
Output:
-
Code - // > given a positive integer N
- // do not have to account for N of 0
- function binaryGap($n)
- {
while (!($n & 1)){$n >>= 1;}$n >>= 1;$max = 0;$current = 0;while ($n){if ($n & 1){$max = max($max, $current);$current = 0;}else{++$current;}$n >>= 1;}return $max;}- return preg_match_all('/1(0+)1/', decbin($n), $matches) ? max(array_map('strlen', $matches[1])) : 0;
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}