Question: https://codility.com/demo/take-sample-test/binary_gap

Question Name: BinaryGap

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def solution(N): max_gap = 0 current_gap = 0 # Skip the tailing zero(s) while N > 0 and N%2 == 0: N //= 2 while N > 0: remainder = N%2 if remainder == 0: # Inside a gap current_gap += 1 else: # Gap ends if current_gap != 0: max_gap = max(current_gap, max_gap) current_gap = 0 N //= 2 return max_gap |

Do you think, mine is a good solution? I got a score of 100 but I’m new to python..

https://codility.com/demo/results/demoEPJM3K-G24/

Yes, your solution is good. Actually my solution is kind of bad. I should use more bit operations rather than math operations.

Hi Sheng, I really appreciate if you can tell me why “K: solution (posted in his link in the comments) has the complexity of Logn. I think it should be O(n) since we see every bit once.

Please advise. Thank you!

Hello!

N is the original number. And the number of bits in it is log2(N).

Guys, please judge my solution https://codility.com/demo/results/demoZK5TWJ-945/. What do you think?

Be confident please! Your solution is pretty good! Only one small suggestion:

Use “return max(b)” instead of “b.sort(); return b[-1]”

The former is O(N), while the latter is O(NlogN).

Here’s my C++ solution for which I get 100%

Hello,

how does it work with tailing zeros.

n=51712=110010100000000 and n=20=10100

codality shows it correct, but the program has no logic when first tailing LSB is zero.

I used this code in my IDE, and it doesn’t work with the two numbers I mentioned, however, codality analysis says your code works perfectly for these two numbers.

I am confused. Would you please tell me how your code works when for it finds zero at LSB in the first cycle of the while loop.

It does consider the tailing zeros. Please refer to the line #12: “else if(ones) gap++;”, while initially “ones” is 0. In other words, for the tailing zeros, the conditions for lines #11 #12 and #15 are all false. Eventually, the program skips the tailing zeros.

My php solution, 100% 😀

Even more simpler with PHP:)

My C solution, 100%: https://codility.com/demo/results/trainingBUPAMC-8KR/

Could you talk me through your soluton please, thanks!

My solution using C#

https://codility.com/demo/results/trainingYTJ5PV-3N7/

where is the code?

Click the link and you will see it.

Here is my javascript solution

https://codility.com/demo/results/trainingYMPTHX-Z4S/

Why do you need all this stuff? What are you trying to check?

is ALWAYS true. Why do you need it?

Try to remove that single line on Codility, and you’ll see the reason 🙂

PS: I do not use Javascript. I do not know why.

In java . is it ok?

Edited by Admin: code was removed.

hi Sashi, please read the guideline before posting code. Besides, for the correctness, why not to try codility’s OJ platform?

What do you think about this solution

I think it’s very Pythonic and you are a master in Python 🙂

Space complexity isn’t O(1) !

You are right 🙂

Javascript Solution

C# solution with Regex and Split

The while loop seems unnecessary.

This works great and can you email me the explanation. Thanks for I appreciate your clean code.

I believe these clean code is self-explanatory. Please try to read it.

//java one

Here’s my way of doing it. Does it look unnecessarily long?

Not so long 🙂

why when n=51712 the answer should be 2 not 9?

>>> bin(51712)

‘0b1100101000000000’

medium1

n=51712=110010100000000_2 and n=20=10100_2 ✘WRONG ANSWER

got 9 expected 2

I see why 51712 is 2 not 9 – ending 1 is missing

this got 100

My javaScript solution:

score: 100 of 100

Sorry. Your submitted code is incomplete. Please read the “Guideline for Comments” firstly. Thanks!

Hi Sheng,

The solutio below (Java) resulted in 100 score. I am not very satisfied with the use of the boolean flag and mathematical functions though.

Would you recommend some amendments?

Thanks!

First of all, please read the “Guideline for Comments” firstly, on the right column.

I briefly check your code. It should work. But there is some space to improve. My two cents are:

1. Rename the variable with more meaningful names, such as “flag” -> “in_gap”;

2. The logic inside for loop could be simplified. It is not so readable as these conditions.

C++ solution:

Please refer to “Guideline for Comments” (in the right column) to post code. Thanks!

Hi Sheng, here is my solution in Javascript.

Thanks for sharing!

Clever ES6 Lucas!

Here’s my 100% C# solution.

Here’s a C# solution using bit shifting.

My 100% C# solution

My solution for Javascript

https://codility.com/demo/results/trainingFEWKVG-QEM/

Here’s my JavaScript solution

Please refer to the “Guideline for Comments” on the right column for posting code. Thanks!

My Java solution (100%)

NOTE from admin: there is a bug. Rajashekar’s following comment fixes it.

using a regular expression: https://codility.com/demo/results/trainingYN5VZA-VPM/

Objective-c solution: Can you comment on Big O Notation Complexity of my solution please?

https://codility.com/demo/results/trainingJX2NTY-WEF/

It should be O(log(N))

Thanks for your contribution! Please also read the “Guideline for Comments” in the right column to post code.

Here is working 100% score with PHP 7

Python solution that gets 100%:

Here is mine solution using strings. My first Python program

I think my solution is simple.

It can be done as oneliner in Python. Not too great to read and understand but gets job done:

Steps explained:

– convert int into binary (as suggested by Python official documentation)

– strip zeros (can be don also with rstrip)

– split at 1 (results a list of strings of zeros and empty strings)

– find max from list (as strings ’00’ > ‘0’ is True, then max returns longest string of zeros)

– find lenght of longest string of zeros

Hey Sheng,

I devised a simpler and faster code which handles trailing zeroes at the beginning of the logic itself.

Java solution that gets 100%: