Binary Search for First and Last Occurrence in Sorted Array

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

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories