🚀 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
Solved LeetCode 3370: Smallest Number With All Set Bits
More Relevant Posts
-
✅Day 54 : Leetcode 3228 - Maximum Number of Operations to Move Ones to the End #60DayOfLeetcodeChallenge 🧩 Problem Statement You are given a binary string s. You can perform the following operation any number of times: Choose an index i such that: i + 1 < s.length s[i] == '1' s[i+1] == '0' Then move this '1' to the right until it reaches the next '1' or the end of the string. Your task is to return the maximum number of such operations that can be performed. ✅ My Approach Instead of simulating every movement (which is slow), I used a counting strategy: 🔹 Key Idea Whenever I see a '1', I increase the count of ones seen so far. Whenever I see a pattern like …10…, this '1' can move past all previous ones, contributing extra operations equal to the number of '1's before it. 🔹 Steps Initialize: ones = 0 → counts how many '1' encountered so far. res = 0 → stores total operations. Traverse the string: If current char is '1' → increment ones. Else if the previous char is '1' (meaning we found 10) → add ones to result. Return res as the maximum operations. This avoids simulation and gives the optimal count directly. ⏱ Time Complexity O(n) — Single scan through the string O(1) space #dsa #leetcode #binarysearch #java #coding #problemsolving #100daysofdsa #interviewpreparation #learningeveryday #codingjourney
To view or add a comment, sign in
-
-
🚀 Day 392 of #500DaysOfCode Today I solved LeetCode 661: Image Smoother 🖼️ This problem was all about applying a 3x3 smoothing filter on an image (represented as a 2D matrix). For each pixel, we calculate the average value of the surrounding pixels — considering only the valid neighbors — and take the floor of the result. It’s a great exercise for mastering matrix traversal and boundary condition handling in Java! 💡 🔍 Key Learnings: How to navigate a 2D grid using directional arrays. Handling edge cells carefully (like corners and borders). Efficiently applying averaging filters using nested loops. 🧠 Example: Input: [[100,200,100],[200,50,200],[100,200,100]] Output: [[137,141,137],[141,138,141],[137,141,137]] 💻 Language: Java 🔹 Difficulty: Easy Every problem like this strengthens my fundamentals in array manipulation and spatial algorithms — one step closer to writing cleaner, more efficient code. #Day392 #LeetCode #Java #CodingChallenge #100DaysOfCode #500DaysOfCode #ProblemSolving #SoftwareEngineering #MatrixAlgorithms
To view or add a comment, sign in
-
-
📌 Day 8/100 - Search Insert Position (LeetCode 35) 🔹 Problem: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be inserted in order. 🔹 Approach: I used a binary search approach for efficiency 🔍 1️⃣ Start with two pointers — low and high. 2️⃣ Find the mid index and compare nums[mid] with the target. 3️⃣ If target equals nums[mid], return mid. 4️⃣ If target is smaller, move the high pointer left. 5️⃣ If target is greater, move the low pointer right. 6️⃣ When the loop ends, low gives the correct insert position. 🔹 Key Learning: Binary Search saves time — reducing O(n) to O(log n)! Understanding the condition when to move left/right is key. Even simple problems sharpen logical precision and boundary handling. Each problem strengthens the logic muscle 🧠 — one step closer to mastering algorithms! 💪 #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #LearnByDoing
To view or add a comment, sign in
-
-
💻 Day 11 of #100DaysOfLeetCode – Rotate Image Today’s challenge was “Rotate Image”, a classic matrix manipulation problem that tests both logic and spatial reasoning. 🔹 Problem Statement: Given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise) — all in place, without using extra memory for another matrix. 🔹 My Approach (in Java): I used a two-step in-place transformation technique: 1️⃣ Transpose the matrix — swap elements across the diagonal (matrix[i][j] ↔ matrix[j][i]). 2️⃣ Reverse each row — to achieve the 90° clockwise rotation. This approach ensures O(1) extra space and O(n²) time complexity, which is optimal for this problem. 🔹 Key Takeaways: ✅ Learned how matrix transformations can be broken down into simpler operations. ✅ Improved understanding of in-place algorithms and memory efficiency. ✅ Reinforced clean coding habits and edge case handling. Every rotation brings me one step closer to mastering problem-solving patterns! 💪 #100DaysOfLeetCode #CodingJourney #RotateImage #Java #DSA #ProblemSolving #LeetCode #CodingChallenge #LearningEveryday
To view or add a comment, sign in
-
-
📅 Day 82 of #100DaysOfLeetCode Problem: Delete Node in a BST (LeetCode #450) Approach: The goal is to delete a node with a specific key from a Binary Search Tree (BST). The deletion process involves two main steps: Search for the node to be deleted. Delete it while maintaining the BST property. There are three cases when deleting a node: Leaf node: Simply remove it. One child: Replace the node with its child. Two children: Find the inorder successor (smallest value in the right subtree), replace the node’s value with it, and delete the successor recursively. Complexity: ⏱️ Time: O(h), where h is the height of the BST. 💾 Space: O(h), recursive call stack. 🔗 Problem Link: https://lnkd.in/dx4XEUgz 🔗 Solution Link: https://lnkd.in/d3-RJ6yJ #LeetCode #100DaysOfCode #BinarySearchTree #Recursion #Java #DSA #Algorithms #ProblemSolving #CodeNewbie #TreeTraversal #StudyWithMe #DailyCoding #BuildInPublic #LearnToCode #CodingJourney
To view or add a comment, sign in
-
-
#97day of #100DaysOfCode 🌳 LeetCode 1382: Balance a Binary Search Tree Today’s challenge was about restoring balance and harmony in a BST 🌿 Given an unbalanced BST, we need to transform it into a height-balanced BST — where no path is too long or too short. 🧠 Approach: 1️⃣ Perform inorder traversal to get a sorted list of all node values. 2️⃣ Build a balanced BST from that sorted list using divide and conquer. 📈 Complexity: Time: O(n) Space: O(n)💬 Learning: Balancing a tree is like balancing life — you can’t remove the nodes, but you can reorder your priorities 🌸 #LeetCode #DSA #BinarySearchTree #CodingChallenge #java #Algorithm #ProblemSolving #LeetCodeDaily #100DaysOfCode
To view or add a comment, sign in
-
-
💡 LeetCode 3467 – Transform Array 💡 Today, I solved LeetCode Problem #3467: Transform Array, which focuses on array manipulation and the use of conditional logic in Java — a neat problem that strengthens core programming fundamentals. ⚙️ 🧩 Problem Overview: You’re given an integer array nums. Your task is to: Replace even numbers with 0 Replace odd numbers with 1 Then, sort the transformed array in ascending order. 👉 Example: Input → nums = [4, 7, 2, 9] Output → [0, 0, 1, 1] 💡 Approach: 1️⃣ Iterate through the array. 2️⃣ Use a ternary operator to transform each element (even → 0, odd → 1). 3️⃣ Sort the array to arrange all zeros before ones. ⚙️ Complexity Analysis: ✅ Time Complexity: O(n log n) — due to sorting. ✅ Space Complexity: O(1) — in-place transformation. ✨ Key Takeaways: Practiced logical thinking and ternary operations in Java. Strengthened understanding of array transformations and sorting. Reinforced the value of writing clean, concise, and efficient code. 🌱 Reflection: Even simple transformation problems like this one sharpen the habit of thinking algorithmically. Consistency in small challenges leads to big growth in problem-solving skills. 🚀 #LeetCode #3467 #Java #ArrayManipulation #LogicBuilding #ProblemSolving #CodingJourney #DSA #CleanCode #ConsistencyIsKey
To view or add a comment, sign in
-
-
Day 35 — Maximize the Minimum Powered City Problem: 2528. Maximize the Minimum Powered City Difficulty: Hard Language: Java Status: Solved This problem was a deep dive into binary search on the answer combined with sliding window logic — a very common yet tricky combo for optimization-based problems. Key Idea: Use binary search to guess the minimum power each city can have. Verify feasibility using a greedy + prefix/sliding window approach to distribute additional power stations efficiently. Carefully maintain a running window sum to ensure we don’t exceed the limit k of extra stations. What I Learned: How to apply binary search beyond sorted arrays — especially on the answer space. The importance of balancing precision and efficiency while handling prefix sums and range updates. Got a better intuition for “can we achieve X?” style feasibility functions. These kinds of problems are perfect for strengthening logic and optimization thinking. #Day35 #100DaysOfCode #LeetCode #DSA #BinarySearch #SlidingWindow #GreedyAlgorithm #Java #ProblemSolving #Algorithms #CodingChallenge #ProgrammingJourney #SoftwareEngineering #CodeEveryday
To view or add a comment, sign in
-
-
🔹 Day 51: Power of Two (LeetCode #231) 📌 Problem Statement: Given an integer n, return true if it is a power of two, otherwise return false. An integer n is a power of two if there exists an integer x such that n == 2^x. ✅ My Approach: I checked whether the number can be continuously divided by 2 until it reaches 1: If n is less than or equal to 0, it’s not a power of two. While n is divisible by 2, divide it by 2. If the result equals 1, then it’s a power of two; otherwise, it’s not. 📊 Complexity: Time Complexity: O(log n) — each division by 2 reduces n exponentially. Space Complexity: O(1) ⚡ Submission Stats: Runtime: 1 ms (Beats 33.54%) Memory: 41.04 MB (Beats 41.60%) 💡 Reflection: This problem strengthened my understanding of how powers of two follow a binary pattern. It also reminded me that bit manipulation (n > 0 && (n & (n-1)) == 0) can offer an even faster alternative. ⚡ #LeetCode #Java #Math #BitManipulation #100DaysOfCode #Day51
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Good intuition!