6/7/25, 11:17 PM Search Insert Position - LeetCode
Probl… Submit Sign in Premium
Description Editorial Solutions Submissions
All Solutions
Easy and Simple C++ approach | Binary search :)
SCAAR
80822 Apr 12, 2023
C++
Approach
binary seaerch approach is damn easy for this question and also easy to think of it.
the array is sorted so we just have to fugure out where should it be placed.
so we just have to figure out the position at which the is less that our eleme
prevoius value
value is more than our element.
Thats how its figured out that it's a binary seach problem.
Complexity
Time complexity: O(log(n))
Space complexity: O(1)
Upvote! It only takes 1 click :)
Code
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int low=0;
int high=[Link]();
int mid;
if(target>nums[high-1]){
return high;
}
while(low<=high){
mid=(low+high)/2;
if(nums[mid]==target){
return mid;
[Link] 1/1