Back to February Back to Year 2026
EASY 22 Feb 2026 View on LeetCode

868. Binary Gap

</> Solution

/**
 * Problem: LeetCode 868 - Binary Gap
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public int binaryGap(int n) {
        int lastPos = -1;
        int maxGap = 0;
        int currPos = 0;

        while (n > 0) {
            if ((n & 1) == 1) {
                if (lastPos != -1) {
                    maxGap = Math.max(maxGap, currPos - lastPos);
                }
                lastPos = currPos;
            }
            n >>= 1;
            currPos++;
        }

        return maxGap;
    }
}

TIME COMPLEXITY

O(log n)

SPACE COMPLEXITY

O(1)

TOPICS

Bit Manipulation Simulation