Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

35 search insert position #67

Merged

Conversation

Dhavisco
Copy link
Contributor

@Dhavisco Dhavisco commented Oct 3, 2023

Solution - Search Insert Position Problem using C++ from LeetCode

This solution is implemented in C++. The approach used to solve this problem efficiently involves using a binary search algorithm:

  1. Initialize two pointers, start and end, to the beginning and end of the sorted array, respectively.
  2. Enter a while loop as long as start is less than or equal to end.
  3. Calculate the middle index, mid, as the average of start and end.
  4. Check if the element at index mid is equal to the target:
    • If they are equal, return mid because the target is found in the array.
    • If the element at mid is less than the target, update start to mid + 1 to search the end half of the array.
    • If the element at mid is greater than the target, update end to mid - 1 to search the start half of the array.
  5. Repeat steps 3-4 until start is greater than end, indicating that the entire array has been searched.
  6. If the target is not found, return start. This represents the index where the target should be inserted to maintain the sorted order.

#65

@iamdestinychild iamdestinychild merged commit b7f4092 into iamdestinychild:main Oct 4, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants