🚀 Day 6/30 – Applying Binary Search Beyond the Obvious Today’s problem involved finding the **k-th missing positive number** in a sorted array. At first glance, it looks like a counting problem. But instead of iterating through numbers one by one, I approached it using **Binary Search** to optimize performance. ### 💡 Key Insight For any index `i`, the number of missing elements before it can be calculated using: `missing = arr[i] - (i + 1)` This observation transforms the problem into a monotonic condition — which makes it perfect for binary search. By narrowing the search space based on how many numbers are missing at mid, I was able to determine the correct position efficiently. ### 📊 Performance ✅ Accepted (All test cases passed) ⚡ 0 ms runtime (100% performance) 💾 Strong memory efficiency ### 📚 What I Learned Binary Search is not just about searching values — it’s about recognizing patterns where the search space can be reduced logically. The real improvement comes from spotting the mathematical relationship inside the problem. Day 6 complete. Consistency is turning into confidence 💪 #Day6 #30DaysOfCode #LeetCode #Java #BinarySearch #Algorithms #DataStructures #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency
Binary Search for Kth Missing Positive Number
More Relevant Posts
-
🚀 Day 12/30 – Mastering Binary Search Patterns Today’s problem required finding the position of a target value in a sorted array — or determining where it should be inserted if it doesn’t exist. Rather than scanning linearly, I implemented a Binary Search approach to maintain optimal performance. 💡 Approach Maintain two pointers: left and right Compute mid carefully to avoid overflow Compare the target with the middle element Narrow the search space accordingly If the element is not found, return the left pointer as the insertion index This pattern ensures: ⏱ O(log n) time complexity 📦 O(1) space complexity 📊 Performance ✅ All test cases passed ⚡ 0 ms runtime (100% performance) 💾 Strong memory efficiency 📚 Key Takeaway Binary Search is more than just finding elements — it’s about identifying boundaries and understanding how search space evolves. Recognizing insertion logic as a natural extension of search strengthens problem-solving intuition. Day 12 complete. The fundamentals are getting sharper every day. #Day12 #30DaysOfCode #LeetCode #Java #BinarySearch #Algorithms #ProblemSolving #SoftwareEngineering #CodingJourney #Consistency #TechGrowth
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 | 𝗖𝗼𝗻𝘃𝗲𝗿𝘁 𝗕𝗶𝗻𝗮𝗿𝘆 𝗡𝘂𝗺𝗯𝗲𝗿 𝗶𝗻 𝗔 𝗟𝗶𝗻𝗸𝗲𝗱 𝗟𝗶𝘀𝘁 𝘁𝗼 𝗜𝗻𝘁𝗲𝗴𝗲𝗿 Day 48 ✅ — Binary logic meets linked list traversal. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟭𝟮𝟵𝟬: Convert Binary Number in a Linked List to Integer (Easy) From LeetCode 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Each node represents a binary digit (0 or 1). The head is the most significant bit. So this isn’t just a linked list problem — it’s a 𝗯𝗶𝗻𝗮𝗿𝘆 𝘁𝗼 𝗱𝗲𝗰𝗶𝗺𝗮𝗹 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻 problem wrapped inside linked list traversal. Two key steps: 1️⃣ Compute the length of the list 2️⃣ Traverse again and apply positional binary weights (2^(length-1)) If a node contains 1, add 2^(position) to the result. Classic bit-weight logic. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 First pass: calculate linked list length 👉 Second pass: • If node value is 1 → add 2^(length-1) • Decrement length 👉 Handle last node separately Time Complexity: O(n) Space Complexity: O(1) Two traversals, constant space, clean logic. 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: This problem reinforces something important: Linked list questions aren’t always about pointer manipulation. Sometimes they test whether you can layer fundamental concepts (like binary math) on top of traversal mechanics. The more I practice, the clearer patterns become: Traversal is routine. The real thinking is in identifying the hidden concept. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g9TyHZGk 𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 ✅ | 𝟱𝟮 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #Binary #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #TimeComplexity #Programming
To view or add a comment, sign in
-
🚀 Day 517 of #750DaysOfCode 🔥 LeetCode 3666 – Minimum Operations to Equalize Binary String (Hard) Today’s problem was a parity + math-based greedy optimization challenge that looks simple at first glance but requires deep observation. 🧠 Problem Summary You are given: A binary string s An integer k In one operation, you must flip exactly k different indices. Goal: Make the entire string equal to '1' using the minimum number of operations. If impossible → return -1. 💡 Key Observations 1️⃣ Let zero = number of '0' in the string. 2️⃣ Each operation flips exactly k bits. 3️⃣ Flipping changes parity (even/odd behavior becomes important). 4️⃣ We must carefully handle: When k == length When parity of k and number of zeros mismatch When operations must be even or odd This becomes a mathematical bound + parity correction problem, not a brute-force simulation. 🛠️ Approach Used Count total zeros. Handle edge cases: If no zeros → answer = 0 If len == k → only one full flip possible Compute minimum operations for: Odd number of operations Even number of operations Adjust based on parity constraints. Return minimum valid answer. Time Complexity: O(n) Space Complexity: O(1) 🎯 What I Learned ✔️ Hard problems often reduce to mathematical constraints ✔️ Parity (even/odd) reasoning is powerful in bit-flip problems ✔️ Always check feasibility conditions before optimizing ✔️ Avoid brute force when constraints go up to 10⁵ This problem really tested logical thinking beyond implementation. On to Day 518 🚀 #LeetCode #Java #DataStructures #Algorithms #ProblemSolving #HardProblems #CodingJourney #750DaysOfCode
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟯𝟴/𝟭𝟬𝟬 | 𝗖𝗼𝗽𝘆 𝗟𝗶𝘀𝘁 𝘄𝗶𝘁𝗵 𝗥𝗮𝗻𝗱𝗼𝗺 𝗣𝗼𝗶𝗻𝘁𝗲𝗿 Day 38 ✅ — When linked lists get complex. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟭𝟯𝟴: Copy List with Random Pointer (Medium) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Deep copy a linked list where each node has a next pointer AND a random pointer that can point to any node. The challenge? You can't just copy nodes one-by-one because the random pointers reference nodes you might not have created yet. Solution: 𝗛𝗮𝘀𝗵 𝗠𝗮𝗽. Two passes: Create all nodes and store old → new mapping Connect next and random pointers using the map 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 First pass: Create clone nodes, map original → clone 👉 Second pass: Set clone.next and clone.random using map 👉 Return cloned head from map Time: O(n), Space: O(n) for the hash map 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Nine linked list problems. This one showed that hash maps aren't just for arrays—they're powerful for pointer-based structures too. The pattern: when you need to track relationships between objects, think hash map. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/gAXPCssx 𝗗𝗮𝘆 𝟯𝟴/𝟭𝟬𝟬 ✅ | 𝟲𝟮 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #HashMap #DeepCopy #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #MediumLevel #Programming
To view or add a comment, sign in
-
🚀 Day 13/100 — Recursive Deconstruction Today’s Challenge: LeetCode 761 - Special Binary String 🧩 Yesterday was about bits; today was about Structural Grammar. Special Binary Strings aren't just 1s and 0s; they are a language. The Realization: A special string is like a valid set of parentheses. 1 is ( and 0 is ). To find the "lexicographically largest" version, you have to break the string into its most basic components, sort them, and put them back together. My 0ms Optimization Strategy: 1. Mental Mapping: Treated the string as a nested structure. If 1...0 is a special string, I stripped the outer layer, solved the inside, and then re-wrapped it. 2. Greedy Reassembly: Used Collections.sort() with a reverse comparator. In binary, "larger" just means the 1s appear as early as possible. 3. Memory Management: Minimized object creation by identifying "split points" first before committing to substring allocations. Reflection: Sometimes the fastest way to solve a problem isn't to iterate forward, but to look at the problem as a recursive tree. Every "Special" chunk is a node that can be reordered to maximize the total value. 13 days down. The logic is getting deeper. 87 days to go. 🛠️ #Java #DSA #LeetCode #100DaysOfCode #Recursion #StringAlgorithms #SoftwareEngineer #CodingJourney #Optimization
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 — 𝗪𝗵𝗲𝗻 𝗦𝘁𝗮𝗰𝗸𝘀 𝗚𝗲𝘁 𝗚𝗿𝗲𝗲𝗱𝘆 Yesterday: Valid Parentheses. Basic stack. Today: Remove Duplicate Letters. Stack + greedy + frequency tracking. Level up. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟯𝟭𝟲: Remove Duplicate Letters (Medium) 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲: Remove duplicate letters so the result is: Smallest in lexicographical order (dictionary order) Contains each letter exactly once Example: "bcabc" → "abc" The trick? Knowing when to remove a character from the stack to make room for a better one. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: Monotonic stack + greedy decisions. 👉 Track frequency: how many times each character appears ahead 👉 Track visited: have we already used this character? 👉 For each character, pop stack if: Stack top is larger (worse for lexicographical order) Stack top appears again later (we can use it then) 👉 Push current character and mark visited Time: O(n), Space: O(1) — only 26 letters max. 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: This combines three patterns: Monotonic stack (maintaining order) Greedy algorithm (making locally optimal choices) Frequency tracking (knowing what's ahead) Solving it wasn't about knowing one technique. It was about combining them. 𝗖𝗼𝗱𝗲: https://lnkd.in/gzw6ACFr 57 down. 43 to go. 𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #MonotonicStack #GreedyAlgorithm #DataStructures #Algorithms #CodingInterview #Programming #Java #MediumLevel #PatternCombination
To view or add a comment, sign in
-
Good Morning LinkedIn Connections ☀️ Continuing my Algorithm Series — Bubble Sort Bubble Sort is one of the simplest sorting algorithms. 🔹 Core Idea: Repeatedly compare adjacent elements and swap them if they are in the wrong order. After each pass, the largest unsorted element “bubbles” to the end. 📊 Time Complexity: • Best Case → O(n) (when optimized & already sorted) • Average Case → O(n²) • Worst Case → O(n²) Space Complexity → O(1) Stable Sorting Algorithm Although inefficient for large datasets, Bubble Sort is excellent for understanding sorting fundamentals and swap logic. Mastering simple algorithms builds a strong foundation for solving complex problems. #DSA #Algorithms #Sorting #Java #CodingInterview
To view or add a comment, sign in
-
-
Day 72/100 – LeetCode Challenge ✅ Problem: #7 Reverse Integer Difficulty: Medium Language: Java Approach: Digit Extraction with Overflow Check Time Complexity: O(log₁₀ x) Space Complexity: O(1) Key Insight: Reverse integer by repeatedly extracting last digit and building result. Critical: Check overflow before final cast using long to detect > Integer.MAX_VALUE or < Integer.MIN_VALUE. Solution Brief: Extracted digits using modulo 10. Built reversed number step by step in long to detect overflow. After loop, divided by 10 to remove extra multiplication. Checked if result fits in 32-bit integer range. Handled sign separately at the end. #LeetCode #Day72 #100DaysOfCode #Math #Java #Algorithm #CodingChallenge #ProblemSolving #ReverseInteger #MediumProblem #Overflow #DigitManipulation #DSA
To view or add a comment, sign in
-
-
Day 8 – DSA Journey | Arrays 🚀 Today’s problem pushed me to think in terms of recursion 🔁, constraints 🧩, and backtracking ⏪ rather than simple iteration. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • 🧠 Sudoku Solver 🔹 Sudoku Solver 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • 🔁 Used backtracking to try all valid possibilities • 📊 Tracked constraints using boolean matrices for rows, columns, and 3×3 boxes • ✅ Placed a number only if it was valid across all constraints 𝐇𝐨𝐰 𝐢𝐭 𝐖𝐨𝐫𝐤𝐬 • 🔍 Find the first empty cell • 🔢 Try numbers from 1–9 • ▶️ Recurse if placement is valid • ⏪ Backtrack if it leads to an invalid state 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • 💡 Backtracking is about choices, recursion, and undoing • ⚡ Pre-tracking constraints avoid repeated checks • 🧠 Clear constraints make recursion easier to reason about • 🛑 Clean base cases prevent infinite recursion 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • ⏱ Time: Exponential (heavily pruned by constraints) • 📦 Space: O(1) (fixed board + recursion stack) 🧠 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Backtracking isn’t brute force — it’s controlled exploration with rules 🎯 On to Day 9 🔁🚀 #DSA #Arrays #Backtracking #Recursion #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 DSA Daily Challenge – Day 15 🧠 724. Find Pivot Index (Easy) Another clean prefix-sum based problem from LeetCode — simple idea, elegant execution. 🔍 Problem Recap We’re given an integer array nums. 👉 A pivot index is where: Sum of elements on the left = Sum of elements on the right If multiple pivots exist → return the leftmost one If none exist → return -1 💡 First Thought Brute force approach would be: For every index Calculate left sum Calculate right sum Compare But that would cost O(n²) ❌ We can do better. 🧠 Core Insight Instead of recalculating sums repeatedly: 1️⃣ First compute total sum of the array 2️⃣ Keep a running left sum while iterating 3️⃣ Derive right sum using: Right Sum = Total Sum − Left Sum − Current Element This avoids recomputation completely. 🔄 Step-by-Step Logic Start with leftSum = 0 Compute totalSum Iterate through the array: Calculate rightSum If leftSum == rightSum → return index Add current element to leftSum If no pivot found → return -1 🔥 Why This Works At every index: We conceptually split the array into: Left | Pivot | Right Instead of calculating both sides repeatedly, we: Maintain left sum dynamically Derive right sum mathematically Each element is processed once. ⚙️ Complexity ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 Pattern to Remember 👉 Prefix Sum 👉 Running Sum Technique 👉 Convert nested loop problems into single-pass solutions 👉 Mathematical transformation to avoid recomputation 🧩 Dry Run Example Array: [1, 7, 3, 6, 5, 6] Total Sum = 28 At index 3 (value = 6): Left Sum = 11 Right Sum = 28 − 11 − 6 = 11 Balanced ✅ Pivot Index = 3 🆚 Pattern Connection ProblemCore IdeaRemove DuplicatesTwo PointersMerge Sorted ArrayTwo Pointers (from back)Find Pivot IndexPrefix / Running Sum See the growth? We’re now combining pointer thinking with mathematical optimization. 💪 Consistency builds pattern recognition. Pattern recognition builds speed. Speed builds confidence. 🚀 Ready for Day 16? #DSA #LeetCode #PrefixSum #Arrays #CodingInterview #Java #100DaysOfCode #ProblemSolving #DataStructures #InterviewPrep
To view or add a comment, sign in
-
Explore related topics
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