Day 13 of Daily DSA 🚀 Solved LeetCode 34: Find First and Last Position of Element in Sorted Array Approach: Applied Binary Search twice to efficiently locate the range of the target element. • First pass finds the leftmost (first) occurrence • Second pass finds the rightmost (last) occurrence A boolean flag helps reuse the same logic for both searches, keeping the code clean and optimal. This ensures the required logarithmic performance on a sorted array. ⏱ Complexity: • Time: O(log n) — two binary searches • Space: O(1) — constant extra space 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 48.50 MB (Beats 11.84%) A perfect example of how modifying binary search slightly can solve range-based problems efficiently. #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
Binary Search for First and Last Occurrence in Sorted Array
More Relevant Posts
-
Day 14 of Daily DSA 🚀 Solved LeetCode 35: Search Insert Position ✅ Approach: Applied classic Binary Search on the sorted array. If the target exists, return its index. If not, the final value of start naturally represents the correct insert position while maintaining sorted order. This problem nicely reinforces how Binary Search can be adapted beyond just “find or not find”. ⏱ Complexity: • Time: O(log n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 44.57 MB (Beats 92.62%) Simple logic, powerful efficiency 💪 #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Day 14 of Daily DSA 🚀 Solved LeetCode 153: Find Minimum in Rotated Sorted Array ✅ Approach: Used Binary Search to locate the minimum element in a rotated sorted array. At each step, compared nums[mid] with nums[end] to determine which half is unsorted and contains the minimum. By shrinking the search space intelligently, the minimum element is found efficiently. A great example of how Binary Search adapts to rotated arrays. ⏱ Complexity: • Time: O(log n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.66 MB (Beats 87.66%) Binary Search mastery keeps leveling up 💪 #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Day 15 of Daily DSA 🚀 Solved LeetCode 540: Single Element in a Sorted Array ✅ Approach: Used Binary Search on indices instead of values. Since elements appear in pairs, the unique element breaks the pairing pattern. By forcing mid to be even and comparing nums[mid] with nums[mid + 1], we can safely discard half of the array each time. ⏱ Complexity: • Time: O(log n) — binary search • Space: O(1) — constant extra space 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 52.96 MB (Beats 57.60%) A neat example of how index properties make binary search even more powerful. #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
#100DaysOfCode 👉 LeetCode #35 – Search Insert Position Binary Search is not just about finding elements 👀 🔹 Idea: Applied Binary Search on a sorted array. If the target is found, return its index. If not found, the `left` pointer itself gives the correct position where the element should be inserted. Time Complexity: O(log n) Space Complexity: O(1) ✔ Strengthened binary search intuition ✔ Understood pointer movement deeply ✔ Learned how insertion logic naturally emerges from search 💡 Takeaway: Binary Search answers more than “found or not” — it also tells where something belongs. Building strong DSA fundamentals in Java, one problem at a time. Open to feedback and better approaches 👋 Day 9 ✅ #LearnInPublic #100DaysOfCode #DSA #Java #BinarySearch #ProblemSolving #SoftwareEngineer #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 86 of My DSA Journey | Subsets – Recursion ➡️ Bit Manipulation 💡 Yesterday, I solved the Subsets problem (LeetCode 78) using recursion (backtracking). Today, I challenged myself to solve the same problem using a different approach — Bit Manipulation 🔥 🔹 What changed? Instead of recursion, I used bit masking to generate all subsets. Every number from 0 to 2^n - 1 represents a subset in binary form. 👉 Core idea: (i & (1 << j)) != 0 This checks whether the j-th element should be included in the subset. 📌 Example: nums = [1,2,3] i = 5 → binary (101) Subset → [1,3] ⚡ Key Learning: One problem can have multiple approaches Recursion = intuitive Bit manipulation = optimized & clever Strengthened my understanding of binary concepts ⏱️ Time Complexity: O(n * 2^n) 📦 Space Complexity: O(2^n) Consistency is the key — learning, applying, and improving every single day 💯 #Day87 #DSA #Java #BitManipulation #Recursion #LeetCode #CodingJourney #PlacementPreparation
To view or add a comment, sign in
-
-
Day 13 of Daily DSA 🚀 Solved LeetCode 33: Search in Rotated Sorted Array ✅ Approach: Used modified Binary Search to handle rotation efficiently. At each step, identified the sorted half of the array and checked whether the target lies within that range. Based on this, adjusted the search boundaries to maintain logarithmic performance. This problem is a great example of how classic binary search logic can be extended with small observations. ⏱ Complexity: • Time: O(log n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.97 MB (Beats 45.43%) #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Imagine searching for a number in a list of 1 million sorted numbers. Would you check them one by one? Or eliminate half of the list every step? 🚀 Day 74/365 — DSA Challenge Solved: Binary Search The Problem You're given a sorted array and a target value. Return the index of the target if it exists. Otherwise return -1. 💡 The Idea Behind Binary Search Binary Search works by dividing the search space in half every step. Steps: 1️⃣ Start with the middle element 2️⃣ If it equals the target → return index 3️⃣ If target is larger → search right half 4️⃣ If target is smaller → search left half Repeat until the element is found. ⏱ Time: O(log n) 📦 Space: O(1) Day 74/365 complete. 💻 291 days to go. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #Algorithms #BinarySearch #LearningInPublic
To view or add a comment, sign in
-
-
Day 16 of Daily DSA 🚀 Solved LeetCode 75: Sort Colors ✅ Approach: Used the Dutch National Flag algorithm with three pointers. start → tracks position for 0s mid → current element end → tracks position for 2s By swapping elements in-place, the array is sorted in one pass without using any built-in sort. ⏱ Complexity: • Time: O(n) — single traversal • Space: O(1) — in-place sorting 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.63 MB (Beats 42.09%) A classic problem that perfectly demonstrates pointer-based thinking. #DSA #LeetCode #Java #BinarySearch #TwoPointers #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
🚀 Day 97 of #100DaysOfCode Solved LeetCode #1022 – Sum of Root To Leaf Binary Numbers ✅ A neat blend of DFS traversal and binary math, turning root-to-leaf paths into numbers. Key Takeaways: -> DFS with an accumulated binary value -> Using left shift (num * 2) to build numbers efficiently -> Correctly identifying leaf nodes for the final sum -> Simple recursion, strong fundamentals 🌳➡️🔢 Language: Java -> Runtime: 0 ms (Beats 100%) ⚡ -> Memory: 43.76 MB Consistency wins. Almost at the finish line 💻🔥 #LeetCode #Java #BinaryTree #DFS #Recursion #BitManipulation #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 15 of DSA practice 🚀 (Binary Search on Answer) Today’s main focus was: 🆕 #875 – Koko Eating Bananas This problem was a great reminder that binary search isn’t just for sorted arrays — it can also be applied on the answer space. Instead of searching for an index, we search for the minimum possible eating speed that satisfies the given constraint. It really strengthened my understanding of: Setting correct search boundaries Designing a valid feasibility check Avoiding off-by-one errors Also revised a few earlier binary search problems to reinforce fundamentals: 🔁 #35, #278, #33, #153 Slowly getting more comfortable with advanced binary search patterns 🔥 #DSA #LeetCode #BinarySearch #ProblemSolving #Java #Consistency #LearningInPublic #100DaysOfCode
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