Alternating Bits LeetCode Challenge

🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gTrw45pn 💡 My thought process: We check each bit one by one and compare it with the previous bit. We start with prevBit initialized to -1 which means we haven't seen any bit yet. We loop until n becomes 0 and in each iteration we get the rightmost bit using bitwise AND with 1. If the current bit equals the previous bit then we immediately return false because the bits are not alternating. Otherwise we save the current bit as the previous bit for the next comparison and remove the rightmost bit by right shifting n by 1 position. If we complete the entire loop without finding two consecutive same bits then we return true indicating all bits are alternating. 👉 My Solution: https://lnkd.in/gQpKAMSi If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories