Skip to content

Commit 050f39d

Browse files
authored
Done using Binary search
1 parent 8fbb8b7 commit 050f39d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Search Insert position

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int searchInsert(vector<int>& nums, int target)
4+
{
5+
int low=0, high = nums.size()-1;
6+
int res=0;
7+
while(low<=high)
8+
{
9+
int mid = low+(high-low)/2;
10+
11+
if(nums[mid] == target)
12+
return mid;
13+
14+
else if(target < nums[mid])
15+
{
16+
17+
high = mid-1;
18+
}
19+
else
20+
{
21+
low=mid+1;
22+
// res = mid;
23+
}
24+
}
25+
return low;
26+
}
27+
};

0 commit comments

Comments
 (0)