Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 868 | Binary Gap | Java | Easy | Bit Manipulation & Bit Scanning | CodeByTushu

Video information, playback and saved actions.

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
Β Β Β Β Β Β 
Videos related to "LeetCode 868 | Binary Gap | Java | Easy | Bit Manipulation & Bit Scanning | CodeByTushu"

Related videos are temporarily unavailable.

Try again, or come back later.