-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path35_insert_position.cpp
62 lines (54 loc) · 1.2 KB
/
35_insert_position.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// LC - 35. Search Insert Position
// 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.
// TC = O(logn)
#include <bits/stdc++.h>
using namespace std;
//use the concept of lower bound => find the smallest index arr[index] >= target
int insert_position(vector<int> &nums, int target){
int l = 0;
int h = nums.size()-1;
int ans = nums.size(); //hypothetical index
while(l <=h) {
int mid = (l+h)/2;
if(nums[mid] >= target) {
ans = mid;
h = mid -1;
}
else{
l = mid +1;
}
}
return ans;
}
/*
TC = O(nlogn)
int insert_position(vector<int> &arr, int k){
int s = 0; int e = arr.size()-1;
while(s<=e){
int mid = s + (e-s)/2;
if(arr[mid] == k){
return mid;;
}
else if(arr[mid] > k){
e = mid-1;
}
else{
s = mid +1;
}
}
//find position in array if target doesnt exist in the array
int count = 0;
for(int i = 0; i<arr.size(); i++){
if(arr[i] < k){
count++;
}
}
return count;
}
*/
int main(){
vector<int> arr ={1, 3, 5, 6};
int k = 2;
cout<<insert_position(arr, k);
return 0;
}