Skip to content

Commit 2207a72

Browse files
authored
Merge pull request #507 from thevijayshankersharma/0035-solution
Add a Solution for Search Insert Position (LeetCode Problem 35)
2 parents 3148748 + 2e834e9 commit 2207a72

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
id: search-insert-position
3+
title: Search Insert Position
4+
difficulty: Easy
5+
sidebar_label: 0038-SearchInsertPosition
6+
tags:
7+
- Array
8+
- Binary Search
9+
---
10+
11+
## Problem Description
12+
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :---------------- | :------------ | :--------------- |
15+
| [Merge Two Sorted Lists](https://leetcode.com/problems/search-insert-position/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/search-insert-position/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
16+
17+
## Problem Description
18+
19+
Given a sorted array `nums` of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
20+
21+
You must write an algorithm with O(log n) runtime complexity.
22+
23+
### Examples
24+
25+
#### Example 1:
26+
27+
- **Input:**
28+
- `nums = [1,3,5,6]`
29+
- `target = 5`
30+
- **Output:** `2`
31+
32+
#### Example 2:
33+
34+
- **Input:**
35+
- `nums = [1,3,5,6]`
36+
- `target = 2`
37+
- **Output:** `1`
38+
39+
#### Example 3:
40+
41+
- **Input:**
42+
- `nums = [1,3,5,6]`
43+
- `target = 7`
44+
- **Output:** `4`
45+
46+
### Constraints
47+
48+
- `1 <= `nums.length` <= 10^4`
49+
- `-10^4 <= `nums[i]` <= 10^4`
50+
- `nums` contains distinct values sorted in ascending order.
51+
- `-10^4 <= `target` <= 10^4`
52+
53+
### Approach
54+
55+
To solve the problem with O(log n) runtime complexity, we can use binary search to find the insertion position of the target value.
56+
57+
1. **Binary Search:**
58+
- Start with low = 0 and high = length of `nums` - 1.
59+
- While `low <= high`, compute mid as (low + high) / 2.
60+
- If the target value is equal to the value at index mid, return mid.
61+
- If the target value is less than the value at index mid, set high = mid - 1.
62+
- If the target value is greater than the value at index mid, set low = mid + 1.
63+
- After the loop, if the target value is not found, return low (or high + 1).
64+
65+
### Solution Code
66+
67+
#### Python
68+
69+
```
70+
class Solution(object):
71+
def searchInsert(self, nums, target):
72+
left, right = 0, len(nums) - 1
73+
74+
while left <= right:
75+
mid = left + (right - left) // 2
76+
if nums[mid] == target:
77+
return mid
78+
elif nums[mid] < target:
79+
left = mid + 1
80+
else:
81+
right = mid - 1
82+
83+
return left
84+
85+
```
86+
87+
#### Java
88+
89+
```
90+
class Solution {
91+
public int searchInsert(int[] nums, int target) {
92+
int low = 0, high = nums.length - 1;
93+
94+
while (low <= high) {
95+
int mid = low + (high - low) / 2;
96+
if (nums[mid] == target) {
97+
return mid;
98+
} else if (nums[mid] < target) {
99+
low = mid + 1;
100+
} else {
101+
high = mid - 1;
102+
}
103+
}
104+
105+
return low;
106+
}
107+
}
108+
```
109+
110+
#### C++
111+
112+
```
113+
class Solution {
114+
public:
115+
int searchInsert(vector<int>& nums, int target) {
116+
int low = 0, high = nums.size() - 1;
117+
118+
while (low <= high) {
119+
int mid = low + (high - low) / 2;
120+
if (nums[mid] == target) {
121+
return mid;
122+
} else if (nums[mid] < target) {
123+
low = mid + 1;
124+
} else {
125+
high = mid - 1;
126+
}
127+
}
128+
129+
return low;
130+
}
131+
};
132+
```
133+
134+
### Conclusion
135+
136+
The above solution efficiently finds the insertion position of a target value in a sorted array using binary search. It achieves a runtime complexity of O(log n), providing an optimal solution to the problem.

0 commit comments

Comments
 (0)