Update appNew update is available. Click here to update.
About
Chitkara University 2026
My Stats
EXP gained
yellow-spark
3040
Level
5 (Champion)
Community stats
Discussions
0
Upvotes
0
Know more
105
Total problems solved
100
Easy
5
Moderate
0
Hard
0
Ninja
Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec

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

profile
chirag
Published On 19-Sep-2023
97 views
0 replies
0 upvotes
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

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