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
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




