# Binary Gap

Posted in

Vinay Khatri
Last updated on June 14, 2024

## Problem

Given an integer ‘n’, find the maximum gap between two occurrences of 1. If no or only 1 exists in the binary representation, return 0.

`6`

`1`

#### Explanation

The binary of 6 is ‘110,’ and the maximum gap between two 1s is 1.

## Approach

We create a ‘count’ variable to count the number of 0s. We can check the Least Significant Bit (LSB) of ‘n’ one by one and keep right shifting it. If LSB is 1, we keep the count variable as 0 and update the answer. If it is 0, then keep incrementing the value of the count variable. How right shift operation work: If the given sequence of bits is: ‘0101’, then in the right shifting operation, the MSB will become 0 and all the remaining bits shift right by 1, resulting in ‘0010’

## Complexity Analysis

The time complexity is O(logN)) with no extra space required.

### C++ Programming

``````#include <bits/stdc++.h>

using namespace std;

// function to get maximum gap between two 1s
int binaryGap(int N) {
int count = -32, ans = 0;

while (N != 0) {
// if the rightmost bit is 1, update the answer
if ((N & 1) == 1) {
ans = max(ans, count);
count = 0;
}
count++;

// right shift N by 1
N >>= 1;
}
return ans;
}

int main() {
int n = 6;

cout << binaryGap(n);
}``````

#### Output

``1``

### Java Programming

``````import java.io.*;

class Solution {
// function to get maximum gap between two 1s
public static int binaryGap(int N) {

int count = -32, ans = 0;

while (N != 0) {

// if the rightmost bit is 1, update the answer
if ((N & 1) == 1) {
ans = Math.max(ans, count);
count = 0;
}

count++;

// right shift N by 1
N >>= 1;

}
return ans;
}

public static void main(String[] args) {

int n = 6;

System.out.println(binaryGap(n));

}
}``````

#### Output

``1``

### Python Programming

``````# function to get maximum gap between two 1s
def binaryGap(n):
ans = count = 0

while n > 0:

# if the rightmost bit is 1, update the answer
if n & 1:
ans = max(ans, count)
count = 1

elif count:
count += 1

# right shift N by 1
n >>= 1

return ans

n = 6

print(binaryGap(n))``````

#### Output

``1``

People are also reading:
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.techgeekbuzz.com/blog/binary-gap/", "fetch_time": 1718332682000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-26/segments/1718198861520.44/warc/CC-MAIN-20240614012527-20240614042527-00708.warc.gz", "warc_record_offset": 919524628, "warc_record_length": 10818, "token_count": 648, "char_count": 2163, "score": 4.0625, "int_score": 4, "crawl": "CC-MAIN-2024-26", "snapshot_type": "latest", "language": "en", "language_score": 0.7050029039382935, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00034-of-00064.parquet"}