Problem Link
LeetCode 34 – Find First and Last Position of Element in Sorted Array
👉 https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
📌 Problem Overview
You are given a sorted array (non-decreasing order) and a target value.
Your task:
- Return the starting and ending index of the target.
- If target does not exist → return
[-1, -1] - Required Time Complexity: O(log n)
Example 1
Example 2
🧠 My First Intuition – Brute Force (O(n))
When I first saw this problem, my thinking was simple:
"Since the array is sorted, I just need to find the first occurrence and the last occurrence."
So I used two loops:
- One loop from left → to find first occurrence
- One loop from right → to find last occurrence
Here is my submitted solution:
✅ Why This Works
- Since the array is sorted, occurrences of the same number are grouped together.
- First loop (reverse) finds last occurrence.
- Second loop (forward) finds first occurrence.
❌ Problem with This Approach
Time Complexity = O(n)
Worst case:
- Target is not present
- We scan entire array twice
But the problem clearly states:
You must write an algorithm with O(log n) runtime complexity.
That means: We must use Binary Search.
🚀 Optimized Approach – Binary Search (O(log n))
💡 Key Intuition
If the array is sorted:
- We can use Binary Search to find the first occurrence
- We can use Binary Search again to find the last occurrence
Instead of stopping when we find the target:
- For first occurrence → continue searching left
- For last occurrence → continue searching right
🧠 Idea Breakdown
1️⃣ Find First Occurrence
When we find target at mid:
- Store index
- Move left →
high = mid - 1 - Because there might be another occurrence before it
2️⃣ Find Last Occurrence
When we find target at mid:
- Store index
- Move right →
low = mid + 1 - Because there might be another occurrence after it
💻 Optimized Code (Binary Search Approach)
🔍 Why This Works
Binary Search normally stops when target is found.
Here, we modify it slightly:
- Keep searching even after finding target.
- Narrow the search space toward the boundary we want.
This guarantees:
- First occurrence → leftmost index
- Last occurrence → rightmost index
⏱ Complexity Analysis
Brute Force Approach
- Time: O(n)
- Space: O(1)
Binary Search Approach
- Time: O(log n)
- Space: O(1)
This satisfies the problem constraint.
🎯 Key Learning from This Problem
This problem teaches an important pattern:
When array is sorted and you need boundaries → Think Binary Search.
It is not just about finding the element.
It is about finding:
- First occurrence (Lower Bound)
- Last occurrence (Upper Bound)
This pattern appears in many interview problems.
📚 Similar Problems to Practice
- https://leetcode.com/problems/binary-search/
- https://leetcode.com/problems/search-insert-position/
- https://leetcode.com/problems/search-in-rotated-sorted-array/
- https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
- https://leetcode.com/problems/sqrtx/
🏁 Final Thoughts
My journey solving this problem:
- First thought → Use two loops (works but O(n))
- Then realized constraint → Must be O(log n)
- Optimized using two Binary Searches
This is how problem solving improves:
- Start with correct solution.
- Then optimize.
- Then recognize patterns.
LeetCode 34 is one of the most important Binary Search boundary problems.
If you master this, you unlock an entire category of advanced Binary Search questions.




