Day 36 :- Binary Search & Range Finding: Find First and Last Position of Element ✅ Today’s DSA session was a great exercise in extending the classic Binary Search to handle multiple occurrences of a target value. I tackled LeetCode 34. Find First and Last Position of Element in Sorted Array, focusing on how to efficiently pinpoint a range within a sorted dataset. The Technical Breakdown: Hybrid Search Strategy: I started with a standard Binary Search to find any instance of the target. This immediately gives us an O(log n) starting point. Linear Expansion: Once the target was found, I used two pointers to expand outward from the meeting point. This identifies the exact boundaries where the target values start and end. Edge Case Handling: The logic naturally handles cases where the target isn't present by returning [-1, -1], and it manages single-element arrays or cases where the target spans the entire array. The Trade-off: While this approach is very intuitive, it can reach O(n) in the worst case (e.g., an array of all identical numbers). It’s a great stepping stone before diving into the pure O(log n) approach of finding the "leftmost" and "rightmost" indices independently! It's a solid reminder that Binary Search isn't just for finding a single value—it's the foundation for navigating any sorted range! 🚀 A huge thanks to my mentor, Anchal Sharma and Ikshit .. for the incredible support and for helping me stay consistent on this journey. Having that accountability makes all the difference! #DSA #Java #100DaysOfCode #BinarySearch #TwoPointers #ProblemSolving #Mentorship #LeetCode #SoftwareEngineering
Binary Search & Range Finding: Efficiently Locate Element in Sorted Array
More Relevant Posts
-
🚀 LeetCode Progress Update | Strengthening Core Logic & DSA Today, I tackled the classic "String to Integer (atoi)" problem on LeetCode — and what initially seemed straightforward turned out to be a great exercise in handling edge cases and writing robust logic. While implementing the solution, I realized how important it is to think beyond the obvious. The problem involves handling multiple real-world scenarios such as: Leading whitespaces Optional '+' or '-' signs Non-numeric characters Overflow and underflow conditions One of the key learnings from this problem was understanding how 32-bit integer limits work. In Java, an int can store values only within the range: -->( -2³¹ to 2³¹ - 1) i.e., -2,147,483,648 to 2,147,483,647 So, while parsing a number from a string, if the value exceeds this range: It must be clamped to Integer.MAX_VALUE (2147483647) Or Integer.MIN_VALUE (-2147483648) To handle this effectively, I used a larger data type during computation and carefully controlled overflow conditions — which gave me deeper insight into how real-world systems prevent crashes and unexpected behavior. 💡 This problem reinforced: The importance of edge-case handling Writing defensive and scalable code How exception handling (like try-catch) plays a role when dealing with numeric limits Solving this medium-level problem (with a relatively low acceptance rate) definitely boosted my confidence and strengthened my problem-solving approach. Looking forward to solving more such challenges and continuously improving my DSA and logical thinking skills! 💪 Big thanks to LeetCode for providing such a powerful platform for learning and growth. #LeetCode #DSA #Java #ProblemSolving #CodingJourney #SoftwareEngineering #Developers #TechGrowth #100DaysOfCode
To view or add a comment, sign in
-
-
Day 61 - LeetCode Journey Solved LeetCode 81: Search in Rotated Sorted Array II (Medium) today — a problem that combines binary search + edge case handling + duplicates. This isn’t just a normal binary search. Here, the array is rotated and may contain duplicates, which makes the decision logic more subtle. 💡 Core Idea: At every step, determine which half is sorted. Then decide whether the target lies in that sorted half. If duplicates block the decision, carefully shrink the search space. ⚡ Key Learning Points: • Applying binary search on a rotated array • Handling duplicate values that break clear ordering • Smart boundary adjustments (low++, high--) • Maintaining efficiency close to O(log n) in most cases The real challenge was not writing the code — It was thinking clearly about all possible scenarios. Problems like this strengthen pattern recognition and deepen understanding of search-based algorithms 💯 ✅ Stronger grip on modified binary search ✅ Better handling of tricky edge cases ✅ Improved logical decision-making Each variation of binary search builds sharper intuition. Still learning. Still improving. Still coding 🚀 #LeetCode #DSA #Java #BinarySearch #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
To view or add a comment, sign in
-
-
Day 74 - LeetCode Journey Solved LeetCode 234: Palindrome Linked List (Easy) today — a problem that combines multiple concepts like two pointers + reversing a linked list. The goal is to check whether the linked list reads the same forward and backward. 💡 Core Idea: Instead of using extra space, we optimize by working directly on the list: Find the middle of the list using slow & fast pointers Reverse the second half of the list Compare both halves node by node If all values match → it’s a palindrome ✔️ ⚡ Key Learning Points: • Using slow & fast pointers to find the middle • Reversing half of the linked list efficiently • Comparing two linked list halves • Achieving O(n) time and O(1) space This problem is a perfect example of combining multiple patterns into one clean solution. It’s not just about solving — it’s about recognizing how previously learned concepts fit together 💯 ✅ Better understanding of linked list traversal ✅ Stronger grip on combining multiple techniques ✅ Improved optimization mindset (space efficiency) These are the kinds of problems that build real confidence in DSA 🚀 #LeetCode #DSA #Java #LinkedList #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
To view or add a comment, sign in
-
-
🚀 Today’s LeetCode Problem — Sort Integers by Number of 1 Bits Let’s solve today’s LC in story mode 🎭 Imagine numbers are contestants in a Binary Costume Contest. Today’s contestants: 👉 [5, 3, 7] But this contest has a twist 👇 👨⚖️ The judge doesn’t care about the number itself first. He cares about… 🎈 How many 1s are in their binary costume 🎭 What Are They Wearing? 🔹 5 → 101 → 🎈🎈 (2 ones) 🔹 3 → 011 → 🎈🎈 (2 ones) 🔹 7 → 111 → 🎈🎈🎈 (3 ones) 🏆 Judge’s Rules 1️⃣ Fewer 1s → Higher priority 2️⃣ Same number of 1s → Smaller number wins 👉 Step 1: 3 & 5 → 2 ones 7 → 3 ones So 7 goes last. 👉 Step 2: Tie between 3 and 5 Smaller number first → 3 before 5 ✅ Final Answer: [3, 5, 7] 💡 How I Solved It I implemented this using: 🔹 Brian Kernighan’s Algorithm for efficient bit counting 🔹 TreeMap to maintain sorted order by bit count + value 🔹 Also compared with built-in bitCount() + custom comparator sorting This ensures: ✔ Efficient bit counting ✔ Clean tie-breaking ✔ Optimal sorting logic If you’re interested, I’ve pushed the complete solution (Kernighan + TreeMap + built-in approach) : https://lnkd.in/gV9HZ9w4 👨💻 Would love feedback from fellow developers 🙌 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #100DaysOfCode #Tech #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 59 - LeetCode Journey Solved LeetCode 41: First Missing Positive (Hard) today — a problem that truly tests deep understanding of arrays, indexing, and optimization. This isn’t just about coding, it’s about thinking in-place, minimizing space, and using constraints smartly. 💡 Core Idea: Instead of using extra space, we treat the array itself as a hash map and place each element at its correct index (value → index mapping). This transforms the problem into finding the first index where the expected value is missing. ⚡ Approach Highlights: • Cyclic placement of elements to their correct positions • Ignoring invalid values (≤ 0 or > n) • Swapping until every valid number is at its right index • Final scan to find the first missing positive 🚀 Why this is powerful: • Achieves O(n) time complexity • Uses O(1) extra space • Demonstrates in-place hashing technique • Strengthens understanding of index manipulation patterns These are the kinds of problems that build real interview-level thinking — not just solving, but optimizing 💯 Every hard problem pushes boundaries a little further. Slow progress is still progress. ✅ Improved understanding of in-place algorithms ✅ Better optimization mindset ✅ Stronger grip on edge cases Still learning. Still improving. Still coding 🚀 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #Algorithms #DataStructures #Arrays #Optimization #InterviewPreparation #KeepCoding #GrowthMindset
To view or add a comment, sign in
-
-
Back on track after a short health break — consistency continues 💪 Day 12 of #100DaysOfCode 🚀 👉 LeetCode #15 – 3Sum (Medium) This problem really tests how well you can optimize brute-force thinking into an efficient solution. 🔹 Approach: First, sort the array Fix one element at a time Use two pointers (left & right) to find pairs with sum -nums[i] Carefully skip duplicates to avoid repeated triplets ⏱ Complexity: Time: O(n²) Space: O(1) (excluding output) 📌 Key Learnings: Sorting can simplify complex problems Two-pointer technique shines after sorting Handling duplicates is as important as core logic Medium problems demand clarity + patience, not shortcuts 💡 Takeaway: Optimization isn’t about writing less code — it’s about thinking smarter, step by step. Building strong DSA fundamentals in Java, one problem at a time. #LearnInPublic #DSA #Java #LeetCode #TwoPointers #ProblemSolving #CodingJourney #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 29/60 — LeetCode Discipline Problem Solved: Longest Common Prefix Difficulty: Easy Today’s problem focused on identifying the longest common starting pattern across multiple strings — a classic exercise in string comparison and iteration. The approach involved checking characters position by position across all strings until a mismatch is found. This reinforces how simple iterative logic can elegantly solve problems involving multiple inputs. It’s a reminder that sometimes, clarity in approach matters more than complexity in code. 💡 Focus Areas: • Strengthened string traversal techniques • Practiced comparing multiple inputs efficiently • Improved understanding of edge cases (empty strings, mismatch handling) • Reinforced early stopping conditions for optimization • Focused on clean and readable logic ⚡ Performance Highlight: Achieved efficient runtime with minimal overhead. Finding common ground across multiple inputs is not just a coding skill — it’s a pattern recognition mindset. #LeetCode #60DaysOfCode #100DaysOfCode #DSA #Strings #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering #Java #Developers #Consistency #TechGrowth #LearnToCode
To view or add a comment, sign in
-
-
Day 80 of DSA Problem Solving Solved LeetCode 876 — Middle of the Linked List 🔥 Today’s problem was all about linked list traversal and using the two-pointer technique efficiently. 🚀 Problem Idea We are given the head of a singly linked list, and the task is to return the middle node of the linked list. If there are two middle nodes, we return the second middle node. 💡 Key Learning The main insight was: If we use two pointers, one moving 1 step at a time and the other moving 2 steps at a time, then by the time the fast pointer reaches the end of the list, the slow pointer will be standing at the middle node. So instead of counting the total number of nodes first, we can directly find the middle in just one traversal. 🧠 Concepts Practiced Two Pointer Technique Linked List Traversal Fast and Slow Pointer Single Pass Traversal Pointer Movement Logic ⏱ Time Complexity Time: O(n) Space: O(1) 📈 Real Journey Behind the Solution At first, this problem looks very basic, and the brute force idea of counting nodes and then reaching the middle can come to mind quickly. But this question teaches a much smarter and cleaner approach using slow and fast pointers. This problem helped me strengthen my understanding of linked list fundamentals and pointer movement, which are extremely important in coding interviews. Every day, I’m realizing that even easy problems can teach very powerful concepts when solved with the right approach. #Day80 #DSA #LeetCode #Java #LinkedList #TwoPointers #CodingJourney #ProblemSolving #CodingDaily
To view or add a comment, sign in
-
-
🚀 Day 30/60 — LeetCode Discipline Problem Solved: 3Sum Closest Difficulty: Medium Today’s problem pushed beyond exact answers and focused on finding the closest possible sum to a given target — a subtle yet powerful variation of the classic 3Sum problem. By sorting the array and using a two-pointer approach, the solution efficiently explores combinations while continuously updating the closest result. This problem highlights how small modifications in logic can significantly change the nature of a problem — from exact matching to optimal approximation. 💡 Focus Areas: • Strengthened two-pointer technique • Practiced working with sorted arrays • Improved handling of optimization conditions • Learned to track and update closest values dynamically • Reinforced problem-solving under constraints ⚡ Performance Highlight: Achieved efficient runtime with optimized traversal. Not every problem asks for perfection — sometimes, the goal is to get as close as possible. #LeetCode #60DaysOfCode #100DaysOfCode #DSA #TwoPointers #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering #Java #Developers #Consistency #TechGrowth #LearnToCode
To view or add a comment, sign in
-
-
Day 76 - LeetCode Journey Solved LeetCode 83: Remove Duplicates from Sorted List (Easy) today — a straightforward problem that strengthens linked list traversal and in-place modification. Since the list is already sorted, duplicate elements will always be adjacent, which makes the approach simpler. 💡 Core Idea: Traverse the list and compare each node with its next node. • If values are equal → skip the next node • If not → move forward This way, we remove duplicates without using extra space. ⚡ Key Learning Points: • Leveraging the sorted property of the list • Modifying linked list in-place • Avoiding unnecessary memory usage • Maintaining O(n) time and O(1) space This is a simple but important pattern that often appears in variations like: Remove all duplicates (not just extra ones) Remove duplicates from unsorted lists (using hashing) ✅ Better understanding of linked list traversal ✅ Stronger control over pointer updates ✅ Improved confidence in in-place operations Simple problems like this help sharpen the fundamentals that matter in bigger questions 🚀 #LeetCode #DSA #Java #LinkedList #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
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