forked from rising-entropy/Leetcode-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35_Search_Insert_Position.cpp
More file actions
33 lines (29 loc) · 1.02 KB
/
35_Search_Insert_Position.cpp
File metadata and controls
33 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*Given a sorted array 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.
You must write an algorithm with O(log n) runtime complexity.*/
/*->Initialize low as 0 and high as nums size - 1
->Calculate mid as (low+high)/2
->While low is less than or equal to high do:
->If target is equal to nums[mid] return mid
->If target is greater than nums[mid] set low = mid + 1
->If target is less than nums[mid] set high = mid - 1
->Return low
*/
#include <iostream>
#include <vector>
using namespace std;
int searchInsert(vector<int> &nums, int target)
{
int low = 0, high = nums.size() - 1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (nums[mid] == target)
return mid;
if (nums[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return low;
}
//https://leetcode.com/problems/search-insert-position/