Binary to Decimal Conversion Challenge

Day 24 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Convert Binary to Decimal We were given a binary number. Task was to convert it into decimal. Binary uses base 2. Decimal uses base 10. So the idea is simple. Each binary digit represents a power of 2. Example: Binary 1011 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 8 + 0 + 2 + 1 = 11 💻 Brute Force Approach 🔹️Take the binary number as a string. 🔹️Start from the rightmost digit. 🔹️Maintain a base variable (starting from 1). 🔹️If the digit is '1', add base to result. 🔹️Multiply base by 2 after each step. 🔹️Continue till all digits are processed. Time Complexity: O(n) Space Complexity: O(1) 💻 Optimal Approach (Using Built-in Functions) Instead of manual conversion, we can use built-in functions. 🔹️Convert binary string directly to integer. 🔹️Specify base 2 during conversion. 🔹️Language handles the calculation internally. Time Complexity: O(n) Space Complexity: O(1) 📚 What I learned today: ▫️Binary numbers are just powers of 2. ▫️Right-to-left traversal helps in base calculations. ▫️Built-in functions can simplify many tasks. ▫️Understanding manual logic still matters for interviews. Day 24 completed. Bit manipulation basics getting clearer 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic

To view or add a comment, sign in

Explore content categories