Prime Set Bits in Binary Representation (LeetCode Easy)

🚀 Day 511 of #750DaysOfCode 🚀 🔢 762. Prime Number of Set Bits in Binary Representation (LeetCode - Easy) Today’s problem was a beautiful mix of bit manipulation + prime checking. 🧠 Problem Summary Given two integers left and right, count how many numbers in that range have a prime number of set bits (1s) in their binary representation. 📌 Example: 21 → Binary: 10101 Set bits = 3 → 3 is prime ✅ 💡 Key Observations We need to: Convert each number to binary. Count the number of set bits. Check if that count is prime. Since right ≤ 10^6, the maximum number of set bits possible is 20 (because 2²⁰ ≈ 10⁶). 👉 So instead of checking prime again and again, we can pre-store prime numbers up to 20. 🔥 What I Practiced Today Using Integer.bitCount() efficiently Understanding binary representations deeply Optimizing prime checks for small ranges Clean looping logic 🎯 Time Complexity O(N * sqrt(K)) Where: N = right - left K ≤ 20 (max set bits) So practically very efficient 🚀 💬 Takeaway Sometimes an “Easy” problem is about combining two simple concepts correctly: 👉 Bit manipulation 👉 Prime number checking Consistency > Difficulty. #750DaysOfCode #Day511 #LeetCode #Java #BitManipulation #PrimeNumbers #ProblemSolving #DataStructuresAndAlgorithms

  • text

To view or add a comment, sign in

Explore content categories