From 32d416d10e420b4f547f57fb5d82d403a003b985 Mon Sep 17 00:00:00 2001 From: Sudhir Date: Tue, 11 Nov 2025 23:24:43 +0530 Subject: [PATCH] Solution #35 - Sudhir - 11/11/25 - commit #1 --- .../searchInsertPosition - Explanation.md | 117 ++++++++++++++++++ .../searchInsertPosition - Solution.cpp | 16 +++ .../searchInsertPosition - Solution.java | 15 +++ .../searchInsertPosition - Solution.py | 13 ++ 4 files changed, 161 insertions(+) create mode 100644 Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Explanation.md create mode 100644 Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.cpp create mode 100644 Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.java create mode 100644 Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.py diff --git a/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Explanation.md b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Explanation.md new file mode 100644 index 0000000..150660d --- /dev/null +++ b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Explanation.md @@ -0,0 +1,117 @@ +# Problem Title + +**Difficulty:** Easy +**Category:** Binary Search, Arrays +**Leetcode Link:** [35. Search Insert Position](https://leetcode.com/problems/search-insert-position/) + +--- + +## πŸ“ Introduction + +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. + +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. + +--- + +## πŸ’‘ Approach & Key Insights + +- The array is sorted β€” so binary search is the best tool here (O(log n) time). +- We look for the **first position** where `nums[i] >= target`. +- If the target is greater than all elements, it should be inserted at the **end** (index = `len(nums)`). +- This ensures correct placement whether or not the target exists in the array. + +--- + +## πŸ› οΈ Breakdown of Approaches + +### 1️⃣ Brute Force / Naive Approach + +- **Explanation:** + - Iterate through the array. + - Return the index where `nums[i] >= target`. + - If target is larger than all elements, return `len(nums)`. +- **Time Complexity:** O(n) β€” linear scan through the array. +- **Space Complexity:** O(1) β€” no extra memory. +- **Example/Dry Run:** + +Example input: `nums = [1, 3, 5, 6], target = 2` + +``` +Index 0: 1 < 2 β†’ continue +Index 1: 3 >= 2 β†’ return 1 +Output: 1 +``` + +--- + +### 2️⃣ Optimized Approach + +- **Explanation:** + - Use binary search to narrow down where the element should be. + - Maintain `left` and `right` pointers. + - If `nums[mid] == target`, return `mid`. + - If `nums[mid] < target`, move `left = mid + 1`. + - Else move `right = mid - 1`. + - When loop ends, `left` will represent the **insert position**. +- **Time Complexity:** O(log n) β€” binary search. +- **Space Complexity:** O(1) β€” constant space. +- **Example/Dry Run:** + +Example input: `nums = [1, 3, 5, 6], target = 5` + +``` +left = 0, right = 3 +mid = 1 (value = 3) β†’ 3 < 5 β†’ left = 2 +mid = 2 (value = 5) β†’ match β†’ return 2 +``` + +Output: 2 + +--- + +## πŸ“Š Complexity Analysis + +| Approach | Time Complexity | Space Complexity | +| ------------- | --------------- | ---------------- | +| Brute Force | O(n) | O(1) | +| Optimized | O(log n) | O(1) | + +--- + +## πŸ“‰ Optimization Ideas + +- Using **binary search built-ins** like `bisect_left` in Python can reduce boilerplate. +- For small arrays (n < 10), linear scan may be equally efficient β€” but binary search is best for scalability. + +--- + +## πŸ“Œ Example Walkthroughs & Dry Runs + +```plaintext +Example 1: +nums = [1,3,5,6], target = 5 +β†’ Found at index 2 + +Example 2: +nums = [1,3,5,6], target = 2 +β†’ Insert before 3 β†’ index 1 + +Example 3: +nums = [1,3,5,6], target = 7 +β†’ Beyond all β†’ insert at end β†’ index 4 +``` + +--- + +## πŸ”— Additional Resources + +- [Binary Search (GeeksforGeeks)](https://www.geeksforgeeks.org/binary-search/) +- [Python bisect module](https://docs.python.org/3/library/bisect.html) +- [Leetcode Binary Search Patterns](https://leetcode.com/explore/learn/card/binary-search/) + +--- + +Author: +Date: 11/11/2025 + diff --git a/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.cpp b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.cpp new file mode 100644 index 0000000..b36d0c5 --- /dev/null +++ b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int searchInsert(vector& nums, int target) { + int left = 0, right = nums.size() - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) + return mid; + else if (nums[mid] < target) + left = mid + 1; + else + right = mid - 1; + } + return left; + } +}; \ No newline at end of file diff --git a/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.java b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.java new file mode 100644 index 0000000..97b6214 --- /dev/null +++ b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int searchInsert(int[] nums, int target) { + int left = 0, right = nums.length - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) + return mid; + else if (nums[mid] < target) + left = mid + 1; + else + right = mid - 1; + } + return left; + } +} \ No newline at end of file diff --git a/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.py b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.py new file mode 100644 index 0000000..ecc46c7 --- /dev/null +++ b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.py @@ -0,0 +1,13 @@ +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) - 1 + + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target: + return mid + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + return left \ No newline at end of file