Skip to content

Commit

Permalink
O(log n) solution for binary gap exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
melvio committed Apr 10, 2021
1 parent 8749a8d commit a20340e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// e.g. n=5 -> n=101 , has sequence of '0' which is of length 1
import assert from "assert";

// O(log n)
function sequence101(n) {
let maxSequenceLength = 0;
let currentSequenceLength = 0
Expand All @@ -10,15 +11,12 @@ function sequence101(n) {
while (n > 0) {
if (n % 2 === 1 && !foundFirst1) {
foundFirst1 = true;
} else if (n % 2 === 0 && !foundFirst1) {
// do nothing
} else if (n % 2 === 0 && foundFirst1) {
currentSequenceLength += 1;
} else if (n % 2 === 1 && foundFirst1) {
maxSequenceLength = Math.max(currentSequenceLength, maxSequenceLength);
currentSequenceLength = 0;
}

n >>= 1;
}
return maxSequenceLength;
Expand All @@ -41,6 +39,8 @@ function testSequence101() {
assert(sequence101(1000) === 1); // 1111101000
assert(sequence101(1160) === 3); // 10010001000
assert(sequence101(1161) === 3); // 10010001001
assert(sequence101(2097153) === 20) // 1000000000000000000001
assert(sequence101(Number.MAX_SAFE_INTEGER) === 0); // 11111111111111111111111111111111111111111111111111111
assert(sequence101(-1) === 0);
console.log("success")
}
Expand Down

0 comments on commit a20340e

Please sign in to comment.