Codility Demo Test Solution[Binary Gap]

Sagar Chauhan
2 min readMay 16, 2020

--

Photo by Ilya Pavlov on Unsplash

Recently, I came across a Codility demo task which I was practicing and it seemed interesting, so I gave it a go ahead. To my surprise,

I scored a perfect 100% on all indexes.

Thus, I thought of sharing its solution here.

First, the problem statement :

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. Write a function that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn’t contain a binary gap.

Solution :

Shameless Plug 🤪:

Hey everyone!

I’m excited to announce the launch of my first book, “Building Serverless Applications on AWS: A Step-by-Step Guide !”

Building Serverless Applications on AWS: A Step-by-Step Guide

You can get my book in multiple formats, including Kindle, paperback (both in India and internationally), and Gumroad.

So, whether you prefer a physical book or a digital copy, you can choose the format that works best for you.

If you wish to buy it, please use the link below:

About the book : https://bit.ly/about-my-book
Kindle : https://bit.ly/buy-kindle-version
Paperback India : https://bit.ly/buy-paperback-version-india
Gumroad : https://bit.ly/buy-on-gumroad
Paperback International : https://bit.ly/buy-paperback-version-international

I’m excited to hear what you think!

Until next time……

Feel free to follow me up on :

Twitter | LinkedIn | Github | Website

--

--

Sagar Chauhan
Sagar Chauhan

Written by Sagar Chauhan

Hustler | Geek | Bootstrapper | PolyMath

No responses yet