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

LeetCode 693 | Binary Number with Alternating Bits | Java | Easy | Bit Manipulation | CodeByTushu

Video information, playback and saved actions.

LeetCode 693 | Binary Number with Alternating Bits | Java | Easy | Bit Manipulation | CodeByTushu

Description: πŸš€ Welcome Back to CodeByTushu!

In today’s video, we solve LeetCode 693 β€” Binary Number with Alternating Bits using Bit Manipulation and Math in Java.

We are given a positive integer named n and need to determine whether its binary representation contains alternating bits, where every pair of adjacent bits has different values, such as 101 or 1010.

Instead of checking every bit individually in a loop, we use a mathematical property of the XOR bitwise operator. When an alternating binary number is shifted one position to the right, each bit in the shifted number differs from its corresponding neighboring bit in the original number.

Computing the bitwise XOR of n and its right-shifted value produces a number x in which every bit from the most significant position down to bit zero is set to one. A binary number consisting entirely of ones has a special property: adding one produces a power of two. These two numbers have no common set bits, so their bitwise AND equals zero.

By using a 64-bit integer where needed to prevent signed 32-bit overflow, we can solve the problem with O(1) time complexity and O(1) auxiliary space using a few bitwise operations.

πŸ”₯ Problem Highlights

βœ… Used the XOR property to compare adjacent bits
βœ… Transformed alternating bits into a consecutive sequence of ones
βœ… Verified the all-ones bitmask using a bitwise AND operation
βœ… Used 64-bit integer promotion where needed to prevent integer overflow
βœ… Achieved O(1) time complexity with a constant number of bitwise operations
βœ… Used O(1) auxiliary space

⏱️ TIMESTAMPS

00:00 Intro and Problem Overview
01:17 Problem Understanding and Constraints
02:40 Right Shift, XOR and Power-of-Two Property
04:42 Bitwise Verification Trick
05:35 Java Solution Walkthrough Line by Line
06:44 Test Cases Walkthrough and Simulation
08:18 Time Complexity Analysis O(1)
08:58 Space Complexity Analysis O(1)
09:29 Outro and Wrap-up

πŸ“‚ Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/18-February/693-Binary-Number-with-Alternating-Bits

β˜• Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/18-February/693-Binary-Number-with-Alternating-Bits/Solution.java

🐍 Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/18-February/693-Binary-Number-with-Alternating-Bits/solution.py

⚑ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/18-February/693-Binary-Number-with-Alternating-Bits/solution.cpp

🌐 JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/18-February/693-Binary-Number-with-Alternating-Bits/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 Tricks
Binary Invariant Patterns
Power of Two Validation
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

Why does the bitwise AND of x and x plus one equal zero when x consists entirely of consecutive ones in binary?

1️⃣ Because adding one to a number consisting entirely of ones changes those ones to zeros and sets the next higher bit to one
2️⃣ Because XOR always returns an even number
3️⃣ Because negative numbers cannot be represented in binary
4️⃣ Because the number of bits in an integer is always a multiple of eight

#LeetCode693 #LeetCode #BinaryNumberWithAlternatingBits #Java #JavaDSA #BitManipulation #Math #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu

πŸ‘‡ Comment the answer below! πŸ”₯Β Close description
Β Β Β Β Β Β 
Videos related to "LeetCode 693 | Binary Number with Alternating Bits | Java | Easy | Bit Manipulation | CodeByTushu"