Optimized Bit Manipulation for Complement of Base 10 Integer on LeetCode

🚀 Day 52 of My Python & DSA Journey Today I revisited Complement of Base 10 Integer on LeetCode — but this time with a more optimized bit manipulation approach! 🔍 Problem Overview: Find the complement of a number by flipping all bits in its binary form. 🧠 Optimized Approach: Instead of converting to binary string, I used bitwise operations: • Create a mask with all bits set to 1 (same length as the number) • Use XOR (^) to flip the bits directly • Special case: when n = 0, return 1 👉 Formula used: (mask - 1) ^ n ⚡ Why this is better: • Avoids string conversion • Works directly on bits (more efficient) • Cleaner and more optimal solution 📊 Complexity: • Time Complexity: O(log n) • Space Complexity: O(1) 💡 Key Learnings: • Deepened understanding of bitwise operators • Learned how to build bit masks dynamically • Understood difference between basic vs optimized solutions Under the Guidance of : Rudra Sravan kumar and Manoj Kumar Reddy Parlapalli #Day52 #Python #LeetCode #BitManipulation #DataStructures #Algorithms #CodingJourney #100DaysOfCode 🚀

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories