Codility: BinaryGap Solution

Lesson 1 Iterations
1function solution(N) {
2 let tempN = N
3 let max = 0
4 let curr = 0
5 let numOfOne = 0
6 while (tempN > 0) {
7 let rmd = Math.floor(tempN % 2)
8 if (rmd === 1) {
9 numOfOne += 1
10 if (numOfOne === 2) {
11 if (curr > max) {
12 max = curr
13 }
14
15 // reset
16 numOfOne = 1
17 curr = 0
18 }
19 } else if (rmd === 0 && numOfOne === 1) {
20 curr += 1
21 }
22 tempN = Math.floor(tempN / 2)
23 }
24 return max
25}

Comments

Loading comments...

Tags

codility

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

Codility: PermCheck

Lesson 4 Counting Elements

Previous Post

HoningJS

Search Posts