LeetCode 868 | Binary Gap | Java | Easy | Bit Manipulation & Bit Scanning | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 868 β Binary Gap using Bit Manipulation and Bit Scanning in Java.
We are given a positive integer named n, and our goal is to find the maximum distance between any two adjacent set bits in its binary representation. Two set bits are separated only by zeros. The distance is the difference between their bit positions. If n contains fewer than two set bits, we return zero.
We scan the binary representation of n one bit at a time, starting from the least significant bit and moving toward the most significant bit. We maintain the position of the previously encountered set bit and track the current bit position. Whenever we find another set bit, we calculate the distance between the current position and the previous position, then update the maximum answer.
For values of n up to 1,000,000,000, this approach requires at most 30 iterations. It achieves O(log n) time complexity and O(1) auxiliary space without converting the number into a string or allocating additional arrays.
π₯ Problem Highlights
β Scanned bits sequentially using right shifts and bitwise AND operations
β Tracked the positions of adjacent set bits dynamically
β Handled edge cases with zero or one set bit
β Avoided string conversion and additional array allocations
β Used a clean and efficient Java implementation
β Achieved O(log n) time complexity and O(1) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro and Problem Overview
01:14 Problem Understanding and Constraints
02:48 Bitwise Streaming and Register Tracking
04:26 Single-Pass Bit Check and Shift Strategy
05:36 Java Solution Walkthrough Line by Line
06:56 Test Cases Walkthrough and Trace Simulation
08:42 Time Complexity Analysis O(log n)
09:39 Space Complexity Analysis O(1)
10:12 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/22-February/868-Binary-Gap
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/22-February/868-Binary-Gap/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/22-February/868-Binary-Gap/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/22-February/868-Binary-Gap/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/22-February/868-Binary-Gap/solution.js
π LeetCode Source Code
π Add the link here
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Bit Manipulation and Bit Scanning
Binary Representation Traversal
In-Place Bitwise Algorithms
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
If n is a positive power of two, such as 16 or 1024, what will the binary gap function return?
1οΈβ£ Zero
2οΈβ£ One
3οΈβ£ The base-two logarithm of n
4οΈβ£ Two
#LeetCode868 #LeetCode #BinaryGap #Java #JavaDSA #BitManipulation #Bitwise #Simulation #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 868 β Binary Gap using Bit Manipulation and Bit Scanning in Java.
We are given a positive integer named n, and our goal is to find the maximum distance between any two adjacent set bits in its binary representation. Two set bits are separated only by zeros. The distance is the difference between their bit positions. If n contains fewer than two set bits, we return zero.
We scan the binary representation of n one bit at a time, starting from the least significant bit and moving toward the most significant bit. We maintain the position of the previously encountered set bit and track the current bit position. Whenever we find another set bit, we calculate the distance between the current position and the previous position, then update the maximum answer.
For values of n up to 1,000,000,000, this approach requires at most 30 iterations. It achieves O(log n) time complexity and O(1) auxiliary space without converting the number into a string or allocating additional arrays.
π₯ Problem Highlights
β Scanned bits sequentially using right shifts and bitwise AND operations
β Tracked the positions of adjacent set bits dynamically
β Handled edge cases with zero or one set bit
β Avoided string conversion and additional array allocations
β Used a clean and efficient Java implementation
β Achieved O(log n) time complexity and O(1) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro and Problem Overview
01:14 Problem Understanding and Constraints
02:48 Bitwise Streaming and Register Tracking
04:26 Single-Pass Bit Check and Shift Strategy
05:36 Java Solution Walkthrough Line by Line
06:56 Test Cases Walkthrough and Trace Simulation
08:42 Time Complexity Analysis O(log n)
09:39 Space Complexity Analysis O(1)
10:12 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/22-February/868-Binary-Gap
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/22-February/868-Binary-Gap/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/22-February/868-Binary-Gap/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/22-February/868-Binary-Gap/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/22-February/868-Binary-Gap/solution.js
π LeetCode Source Code
π Add the link here
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Bit Manipulation and Bit Scanning
Binary Representation Traversal
In-Place Bitwise Algorithms
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
If n is a positive power of two, such as 16 or 1024, what will the binary gap function return?
1οΈβ£ Zero
2οΈβ£ One
3οΈβ£ The base-two logarithm of n
4οΈβ£ Two
#LeetCode868 #LeetCode #BinaryGap #Java #JavaDSA #BitManipulation #Bitwise #Simulation #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
