30 Days of Code [Day 24]
QUESTION : 35. Search Insert Position
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define fo(i,s,n) for(int i=s;i<n;++i)
#define of(i,s,n) for(int i=s-1;i>=n;--i)
#define fv(V) for( auto &it : V )
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
fastio;
int l =0, r = nums.size()-1, mid;
while(l <= r){
mid = l + (r-l)/2;
if(nums[mid] == target) return mid;
else if(nums[mid] < target) l = mid+1;
else r = mid-1;
}
if(target < nums[mid]) return mid;
else return mid+1;
}
};