Chitkara University 2026
My Stats
EXP gained
3040
Level
5 (Champion)
Community stats
Discussions
0
0
Know more
105
Total problems solved
100
Easy
5
Moderate
0
Hard
0
Ninja

Current streak:

0 days

Longest streak:

6 days

Less

More

Achievements
2
Ronin
Topics
Binary Search
Arrays
1
Samurai
Guided path
Pointers
Discussions
Binary search
Interview problems

int searchInsert(vector<int>& arr, int m)

{

// Write your code here.

int low=0;

int high=arr.size()-1;

int mid=low+(high-low)/2;

while(low<=high)

{

if(arr[mid]==m)

{

return mid;

}

else if(arr[mid]>m)

{

high=mid-1;

}

else{

low=mid+1;

}

mid=low+(high-low)/2;

}

return mid;

}

This is simple binary search code but i am updating my mid at last so whenever last so my mid got updated before the end of while loop and i return that mid

chirag
Published On 19-Sep-2023
97 views
0 replies
Binary search
Interview problems

int searchInsert(vector<int>& arr, int m)

{

// Write your code here.

int low=0;

int high=arr.size()-1;

int mid=low+(high-low)/2;

while(low<=high)

{

if(arr[mid]==m)

{

return mid;

}

else if(arr[mid]>m)

{

high=mid-1;

}

else{

low=mid+1;

}

mid=low+(high-low)/2;

}

return mid;

}

This is simple binary search code but i am updating my mid at last so whenever last so my mid got updated before the end of while loop and i return that mid

chirag
Published On 19-Sep-2023
97 views
0 replies