|
| 1 | +# Problem Title |
| 2 | + |
| 3 | +**Difficulty:** Easy |
| 4 | +**Category:** Binary Search, Arrays |
| 5 | +**Leetcode Link:** [35. Search Insert Position](https://leetcode.com/problems/search-insert-position/) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 📝 Introduction |
| 10 | + |
| 11 | +Given a sorted array of **distinct integers** and a target value, the task is to return the **index** if the target is found. If not, return the index where it **would be inserted** in order. |
| 12 | + |
| 13 | +This is a **classic binary search** problem with a small twist — instead of just finding the element, we also handle where it would fit in if it’s not present. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 💡 Approach & Key Insights |
| 18 | + |
| 19 | +- The array is sorted — so binary search is the best tool here (O(log n) time). |
| 20 | +- We look for the **first position** where `nums[i] >= target`. |
| 21 | +- If the target is greater than all elements, it should be inserted at the **end** (index = `len(nums)`). |
| 22 | +- This ensures correct placement whether or not the target exists in the array. |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## 🛠️ Breakdown of Approaches |
| 27 | + |
| 28 | +### 1️⃣ Brute Force / Naive Approach |
| 29 | + |
| 30 | +- **Explanation:** |
| 31 | + - Iterate through the array. |
| 32 | + - Return the index where `nums[i] >= target`. |
| 33 | + - If target is larger than all elements, return `len(nums)`. |
| 34 | +- **Time Complexity:** O(n) — linear scan through the array. |
| 35 | +- **Space Complexity:** O(1) — no extra memory. |
| 36 | +- **Example/Dry Run:** |
| 37 | + |
| 38 | +Example input: `nums = [1, 3, 5, 6], target = 2` |
| 39 | + |
| 40 | +``` |
| 41 | +Index 0: 1 < 2 → continue |
| 42 | +Index 1: 3 >= 2 → return 1 |
| 43 | +Output: 1 |
| 44 | +``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### 2️⃣ Optimized Approach |
| 49 | + |
| 50 | +- **Explanation:** |
| 51 | + - Use binary search to narrow down where the element should be. |
| 52 | + - Maintain `left` and `right` pointers. |
| 53 | + - If `nums[mid] == target`, return `mid`. |
| 54 | + - If `nums[mid] < target`, move `left = mid + 1`. |
| 55 | + - Else move `right = mid - 1`. |
| 56 | + - When loop ends, `left` will represent the **insert position**. |
| 57 | +- **Time Complexity:** O(log n) — binary search. |
| 58 | +- **Space Complexity:** O(1) — constant space. |
| 59 | +- **Example/Dry Run:** |
| 60 | + |
| 61 | +Example input: `nums = [1, 3, 5, 6], target = 5` |
| 62 | + |
| 63 | +``` |
| 64 | +left = 0, right = 3 |
| 65 | +mid = 1 (value = 3) → 3 < 5 → left = 2 |
| 66 | +mid = 2 (value = 5) → match → return 2 |
| 67 | +``` |
| 68 | + |
| 69 | +Output: 2 |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## 📊 Complexity Analysis |
| 74 | + |
| 75 | +| Approach | Time Complexity | Space Complexity | |
| 76 | +| ------------- | --------------- | ---------------- | |
| 77 | +| Brute Force | O(n) | O(1) | |
| 78 | +| Optimized | O(log n) | O(1) | |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## 📉 Optimization Ideas |
| 83 | + |
| 84 | +- Using **binary search built-ins** like `bisect_left` in Python can reduce boilerplate. |
| 85 | +- For small arrays (n < 10), linear scan may be equally efficient — but binary search is best for scalability. |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## 📌 Example Walkthroughs & Dry Runs |
| 90 | + |
| 91 | +```plaintext |
| 92 | +Example 1: |
| 93 | +nums = [1,3,5,6], target = 5 |
| 94 | +→ Found at index 2 |
| 95 | +
|
| 96 | +Example 2: |
| 97 | +nums = [1,3,5,6], target = 2 |
| 98 | +→ Insert before 3 → index 1 |
| 99 | +
|
| 100 | +Example 3: |
| 101 | +nums = [1,3,5,6], target = 7 |
| 102 | +→ Beyond all → insert at end → index 4 |
| 103 | +``` |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +## 🔗 Additional Resources |
| 108 | + |
| 109 | +- [Binary Search (GeeksforGeeks)](https://www.geeksforgeeks.org/binary-search/) |
| 110 | +- [Python bisect module](https://docs.python.org/3/library/bisect.html) |
| 111 | +- [Leetcode Binary Search Patterns](https://leetcode.com/explore/learn/card/binary-search/) |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +Author: |
| 116 | +Date: 11/11/2025 |
| 117 | + |
0 commit comments