Bit Manipulation: Number of 1 Bits (Hamming Weight)

🔥 DSA Challenge – Day 136/360 🚀 📌 Topic: Bit Manipulation 🧩 Problem: Number of 1 Bits (Hamming Weight) Problem Statement: Given an integer, return the number of set bits (1’s) present in its binary representation. 🔍 Example: Input: 6 Output: 2 Explanation: Binary of 6 → 110 → 2 set bits 💡 Optimized Approach (Brian Kernighan’s Algorithm): Instead of checking each bit one by one, we use a smart trick: n & (n - 1) removes the rightmost set bit in each iteration This reduces the number of operations to the number of set bits only ⚡ Time Complexity: O(k) (k = number of set bits) ⚡ Space Complexity: O(1) 🚀 Key Takeaway: Using bit manipulation can drastically optimize performance compared to brute force approaches. Always look for patterns in binary operations! #DSA #Coding #Java #BitManipulation #136DaysOfCode #LeetCode #ProblemSolving #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories