Solved LeetCode 3370: Smallest Number With All Set Bits

🚀 LeetCode 3370 — Smallest Number With All Set Bits Today I solved an interesting problem that tested my bit manipulation intuition 🧠 🧩 Problem: Given a positive integer n, find the smallest number x ≥ n such that the binary representation of x contains only set bits (1s). 💡 My Thought Process: I realized that numbers with all bits set follow a simple pattern: 1 -> 1 3 -> 11 7 -> 111 15 -> 1111 31 -> 11111 Each is basically (power of 2) - 1 🔥 So my goal became: ➡️ Move n to the next power of two, ➡️ Then subtract 1, to make all bits below it set. But instead of doing it mathematically, I wanted to do it bit-by-bit 👇 🔍 How it works: 1️⃣ From MSB to LSB, keep only the first 1 bit and clear the rest. 2️⃣ Left shift to reach the next power of 2. 3️⃣ Subtract 1 to make all bits set. Example: n = 10110 (22) → n = 10000 → n << 1 = 100000 → n - 1 = 11111 (31) ✅ 💬 Takeaway: Sometimes, the most elegant solutions are the ones where you play directly with bits rather than formulas. Bit manipulation is like solving puzzles at the binary level — small details, huge insights ⚙️ #LeetCode #Java #BitManipulation #DSA #ProblemSolving #CodingJourney #100DaysOfCode #LearnEveryday

  • text

To view or add a comment, sign in

Explore content categories