Binary Search for First & Last Occurrence in Sorted Array

🚀 Day 25 / 180 – DSA with Java 🚀 📘 Topic Covered: Binary Search (First & Last Occurrence) 🧩 Problem Solved: Find First and Last Position of Element in Sorted Array Problem: Given a sorted array and a target value, find the starting and ending position of the target in O(log n) time. If not found, return [-1, -1]. Approach: Used Binary Search twice — • First to find the leftmost (first) occurrence • Then to find the rightmost (last) occurrence By carefully adjusting the search boundaries, ensured both positions were located efficiently. Key Learning: ✔️ Extending binary search for range queries ✔️ Handling boundary conditions carefully ✔️ Maintaining O(log n) time complexity If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #BinarySearch #ProblemSolving #Consistency

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories