```
int start=0, end = nums.length-1, pos=-1, mid;
while(start<=end)
{
mid = start + (end-start)/2;
if(nums[mid] == target)
return mid;
else if(nums[start]<target && end-start==0)
return start+1;
else if(nums[start]>target && end-start==0)
return start;
else if(nums[mid] > target)
end = mid-1<0 ? 0:end-1;
else if (nums[mid] < target)
start = mid+1;
}
return -1;
}```
```