Binary Search in Sorted Array: Find First & Last Position

🚀 50 Important Coding Questions – Question 9/50 🔹 Find First and Last Position of Element in Sorted Array | LeetCode (Medium) A classic Binary Search variation that tests how well you understand search boundaries 👇 📌 Problem Statement Given a sorted array of integers and a target value, find the starting and ending position of the target. If the target is not found, return [-1, -1]. 💡 Optimized Approach (Modified Binary Search) Use binary search twice First pass → find the first occurrence Second pass → find the last occurrence Narrow search space even after finding the target ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) ✅ Why this problem is important? ✔ Strengthens binary search mastery ✔ Introduces boundary-based searching ✔ Very common in FAANG & product-based interviews ✔ Foundation for lower/upper bound problems 📌 LeetCode Result: ✔ Accepted ⚡ Runtime: 0 ms (Beats 100%) 📊 Memory Efficient 🔔 This is Question 9 of my “50 Important Coding Questions” series. Follow for daily DSA problems, optimized approaches, and interview-ready logic 💻✨ 👉 Question 10 coming soon… #DSA #LeetCode #BinarySearch #Arrays #CPlusPlus #CodingInterview #ProblemSolving #50ImportantQuestions

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories