Day 12/30 – LeetCode streak Today’s problem: Number of Steps to Reduce a Number in Binary Representation to One Rules are simple: if the number is even, divide by 2; if it’s odd, add 1. But doing this by simulating the whole number each time is slow and messy. The trick is to walk the binary string from right to left and just count how many operations each bit contributes, while carrying a 1 when we “add 1”: - Ignore index 0 (most significant bit); we stop at 'i > 0'. - For each bit, compute 'bit = (s.charAt(i) - '0') + carry'. - If 'bit' is '1', it’s effectively odd → we need '+1' then '/2' → 'steps += 2' and set 'carry = 1'. - If 'bit' is '0' or '2', it’s even → just '/2' → 'steps += 1', 'carry' stays as is. - After the loop, if there’s still a carry, add one more step because we effectively turned something like '"111"' into '"1000"' and need one last divide to reach '1'. Day 12 takeaway: Instead of repeatedly mutating the number, counting how many times each bit causes “+1 then /2” vs “just /2” gives an O(n), no-conversion solution that feels much cleaner once the carry idea clicks. #leetcode #dsa #java #bitmanipulation #consistency
LeetCode Day 12: Binary Number Steps to One
More Relevant Posts
-
Day 14/30 – LeetCode streak Today’s problem: Concatenation of Consecutive Binary Numbers You need the decimal value of '1' + '2' + '3' + ... + 'n' written in binary back-to-back, all under mod (10^9 + 7). Core trick: treat “concatenate in binary” as shift + OR: * When you append 'i' to the right, you’re really shifting the current result left by 'bits(i)' and OR-ing 'i' into the free space. * The number of bits only increases when 'i' hits a power of two (1, 2, 4, 8, …), so you just track 'bits' and bump it whenever '(i & (i - 1)) == 0'. Day 14 takeaway: Once you see that “stick this binary to the right” is the same as “shift by its bit length and OR”, the whole problem becomes a clean for-loop plus the power-of-two trick—no string building or big integer juggling needed. #leetcode #dsa #java #bitmanipulation #consistency
To view or add a comment, sign in
-
-
Day 117 🚀 | LeetCode Progress Solved 3 problems today and strengthened array manipulation skills 💪 📌 Problem 1: Sort Array By Parity (#905) – 🟢 Easy 👉 Two-pointer / index swapping technique 👉 Place even numbers at the front while iterating 👉 In-place solution with O(1) extra space 👉 Time Complexity: O(n) Efficient way to partition arrays without extra memory ⚡ 📌 Problem 2: Find All Numbers Disappeared in an Array (#448) – 🟢 Easy 👉 Index marking technique using negative values 👉 Mark visited indices by flipping the sign 👉 Positive indices at the end represent missing numbers 👉 Time: O(n) | Space: O(1) (excluding result list) A clever trick to use the array itself as a hash map 🧠 📌 Problem 3: Minimum Changes to Make an Alternating Binary String (#1758) – 🟢 Easy 👉 Compare with two possible patterns: "010101..." and "101010..." 👉 Count mismatches for both patterns 👉 Return the minimum operations needed 👉 Time Complexity: O(n) Simple logic but great practice for pattern-based string problems 🔍 Small improvements every day lead to big progress over time 📈 #Day117 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #GeekStreak #100DaysOfCod
To view or add a comment, sign in
-
Day 8 Today I solved “Check if Binary String Has at Most One Segment of Ones” on LeetCode. At first glance, the problem looks simple: Given a binary string, check whether it contains only one continuous segment of '1's. Example: "110" → Valid (one segment of 1s) "1001" → Invalid (two separate segments of 1s) While solving it, I realized an interesting observation: 👉 If a binary string has more than one segment of 1s, the pattern "01" must appear before another "1". So instead of counting segments explicitly, we can simply check whether '01' appears and is followed by another '1'. This turns the problem into a very efficient linear scan with O(n) time complexity. 💡 Key takeaway: Sometimes the simplest solution comes from recognizing patterns in the string rather than simulating the whole process. Also happy to see my solution running at 0 ms runtime (100% faster) 🚀 #LeetCode #DSA #ProblemSolving #Java #CodingJourney #BinaryStrings
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfLeetCode Today I solved "Container With Most Water" problem on LeetCode. 🔹 Difficulty: Medium 🔹 Concept Used: Two Pointer Technique 🔹 Language: Java 📌 Problem Summary: Given an array representing heights of vertical lines, the goal is to find two lines that together with the x-axis can contain the maximum amount of water. 💡 Approach: Instead of checking all possible pairs (O(n²)), I used the Two Pointer approach which reduces the time complexity to O(n). Steps: 1️⃣ Start with two pointers at the beginning and end of the array. 2️⃣ Calculate the area using the smaller height. 3️⃣ Move the pointer with the smaller height inward. 4️⃣ Track the maximum area during each step. 📊 Result: ✅ Runtime: 5 ms (Beats 81.86%) ✅ Memory: Beats 95.54% This problem was a great exercise to understand optimization using two pointers instead of brute force. Consistency is the key — moving forward one problem at a time! 💪 #LeetCode #100DaysOfCode #Java #DSA #CodingChallenge #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 20/30 – LeetCode streak Problem: Check if Binary String Has at Most One Segment of Ones A valid string can have ones only in a single continuous block, and the input has no leading zeros, so it always starts with '1'. Core idea Since 's' starts with '1', any second segment of ones must look like: '1...1 0...0 1...1' → which necessarily contains "01" at the point where ones restart. So if "01" appears anywhere, there are at least two segments of ones → return false. If "01" never appears, all ones are in a single prefix (possibly followed by zeros), so return true. This reduces to a single substring check in 'O(n)' time and 'O(1)' space. Day 20 takeaway: Instead of explicitly counting segments, spotting the forbidden pattern "01" turns the whole logic into a one-line string check that’s still fully correct and optimal. #leetcode #dsa #java #strings #consistency
To view or add a comment, sign in
-
-
🚀 Day 531 of #750DaysOfCode 🚀 💻 LeetCode 1415 — The K-th Lexicographical Happy String of Length n Today’s problem was a great practice of Backtracking + Recursion + Lexicographical order generation. We are given a length n and a number k, and we need to find the k-th happy string in sorted order. 🔹 A happy string means: Only contains characters → a, b, c No two adjacent characters are same Example: Valid → "abc", "acb", "bab" Invalid → "aa", "abb", "bcc" The challenge was to generate all valid strings in lexicographical order and return the k-th one. 💡 Approach Used: Backtracking (DFS) Try characters in order → a, b, c Skip same adjacent character Stop early when k-th string found This problem improved my understanding of: ✔ Recursion ✔ Backtracking ✔ String generation ✔ Lexicographical traversal ✔ Early stopping optimization Consistency is the key — one problem every day, no excuses. 🔥 #750DaysOfCode #Day531 #LeetCode #Java #Backtracking #Recursion #DSA #CodingJourney #SoftwareEngineer #PlacementPreparation
To view or add a comment, sign in
-
-
#Day32 of #365DaysOfCode Today’s LeetCode Practice: 🔹 Container With Most Water (LeetCode 11) Solved a classic two-pointer optimization problem where the goal is to find two lines that together form a container holding the maximum water. 💡 Key Insight: Start with two pointers at both ends. Calculate area using: width × min(height[left], height[right]) Move the pointer with the smaller height inward, because the smaller height limits the water capacity. This guarantees exploring better possibilities efficiently. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Consistency > Motivation. Day by day, improving problem-solving and logical thinking skills #LeetCode #ProblemSolving #Java #CodingJourney #FutureEngineer #Consistency #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 30of LeetCode Odd Even Linked List — Clean Pointer Manipulation! Just solved a really interesting linked list problem that tests how well you understand pointer manipulation 👇 🔹 Problem: Rearrange a linked list such that all nodes at odd indices come first, followed by nodes at even indices — while maintaining their relative order. 👉 Example: Input: 1 → 2 → 3 → 4 → 5 Output: 1 → 3 → 5 → 2 → 4 💡 Key Insight: Instead of using extra space, we can split the list into: an odd-index list an even-index list Then simply connect them at the end! 🧠 Approach: Maintain two pointers: odd and even Keep track of evenHead Rearrange links in one traversal Attach even list after odd list ⚡ Complexity: Time: O(n) Space: O(1) (in-place) 🎯 What I learned: Mastering linked lists is all about pointer control Problems that look tricky often have simple in-place solutions Always think in terms of structure, not values #LeetCode #DataStructures #LinkedList #Java #CodingInterview #100DaysOfCode #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
After reading and understanding the question and the test cases for LeetCode 1689 – Partitioning Into Minimum Number of Deci-Binary Numbers, my first thought was to build the sum using deci-binary numbers (0s and 1s) and then count how many numbers are required. That approach felt natural because Example 1 itself explains the solution like this: Input: n = "32" Output: 3 Explanation: 10 + 11 + 11 = 32 So my thinking was to follow the same idea — construct the sum first and then count the numbers. But when I looked at Example 3: Input: n = "27346209830709182346" Output: 9 and noticed the constraint: 1 <= n.length <= 10^5 it became clear that converting the string into numeric types or constructing the sum directly would not scale given the constraints. After carefully going through the hints and the solution, I understood the key insight: Each deci-binary number can contribute only 0 or 1 per digit. So if any digit in the string is 9, we need at least 9 deci-binary numbers to form that digit. Since all digits must be satisfied together, the maximum digit in the string decides the minimum number of deci-binary numbers required. This problem was a good reminder that sometimes the challenge isn’t coding — it’s thinking about constraints the right way. #LeetCode #ProblemSolving #DSA #Learning #Java
To view or add a comment, sign in
-
-
Day 22 of Daily DSA 🚀 Solved LeetCode 66: Plus One ✅ Approach: Treat the array as a number and simulate addition from the last digit: • If the last digit is not 9, increment and return • Handle carry by setting 9 → 0 and moving left • If all digits are 9, create a new array with leading 1 This avoids converting the array into an integer and handles large numbers safely. ⏱ Complexity: • Time: O(n) • Space: O(1) (extra array only when overflow happens) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.58 MB (Beats 38.49%) A simple problem that tests edge-case thinking 💡 #DSA #LeetCode #Java #ProblemSolving #DailyCoding #Consistency
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
Keep going bud! Do graph too after a while They are not as difficult as people tend to believe All the best 😇