-
-
Notifications
You must be signed in to change notification settings - Fork 736
Add solution for Challenge 21 by Johrespi #655
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||
| package main | ||||
|
|
||||
| import ( | ||||
| "fmt" | ||||
| ) | ||||
|
|
||||
| func main() { | ||||
| // Example sorted array for testing | ||||
| arr := []int{1, 3, 5, 7, 9, 11, 13, 15, 17, 19} | ||||
|
|
||||
| // Test binary search | ||||
| target := 7 | ||||
| index := BinarySearch(arr, target) | ||||
| fmt.Printf("BinarySearch: %d found at index %d\n", target, index) | ||||
|
|
||||
| // Test recursive binary search | ||||
| recursiveIndex := BinarySearchRecursive(arr, target, 0, len(arr)-1) | ||||
| fmt.Printf("BinarySearchRecursive: %d found at index %d\n", target, recursiveIndex) | ||||
|
|
||||
| // Test find insert position | ||||
| insertTarget := 8 | ||||
| insertPos := FindInsertPosition(arr, insertTarget) | ||||
| fmt.Printf("FindInsertPosition: %d should be inserted at index %d\n", insertTarget, insertPos) | ||||
| } | ||||
|
|
||||
| // BinarySearch performs a standard binary search to find the target in the sorted array. | ||||
| // Returns the index of the target if found, or -1 if not found. | ||||
| func BinarySearch(arr []int, target int) int { | ||||
| // TODO: Implement this function | ||||
| low := 0 | ||||
| high := len(arr) - 1 | ||||
|
|
||||
| for low <= high { | ||||
| mid := low + (high - low)/2 | ||||
| if target == arr[mid]{ | ||||
| return mid | ||||
| } else if target > arr[mid]{ | ||||
| low = mid + 1 | ||||
| } else { | ||||
| high = mid - 1 | ||||
| } | ||||
| } | ||||
|
|
||||
| return -1 | ||||
| } | ||||
|
|
||||
| // BinarySearchRecursive performs binary search using recursion. | ||||
| // Returns the index of the target if found, or -1 if not found. | ||||
| func BinarySearchRecursive(arr []int, target int, left int, right int) int { | ||||
| // TODO: Implement this function | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove TODO comment. The TODO comment should be removed as the function is now implemented. Apply this diff: - // TODO: Implement this function📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||
|
|
||||
| mid := left + (right - left)/2 | ||||
|
|
||||
| if left > right{ | ||||
| return -1 | ||||
|
|
||||
| } | ||||
|
|
||||
| if target == arr[mid]{ | ||||
| return mid | ||||
| } else if target > arr[mid]{ | ||||
| return BinarySearchRecursive(arr, target, mid + 1, right) | ||||
| } else { | ||||
| return BinarySearchRecursive(arr, target, left, mid - 1) | ||||
| } | ||||
| } | ||||
|
|
||||
| // FindInsertPosition returns the index where the target should be inserted | ||||
| // to maintain the sorted order of the array. | ||||
| func FindInsertPosition(arr []int, target int) int { | ||||
| // TODO: Implement this function | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove TODO comment. The TODO comment should be removed as the function is now implemented. Apply this diff: - // TODO: Implement this function📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||
|
|
||||
| left := 0 | ||||
| right := len(arr) - 1 | ||||
|
|
||||
| for left <= right { | ||||
| mid := left + (right - left)/2 | ||||
| if target > arr[mid] { | ||||
| left = mid + 1 | ||||
| } else { | ||||
| right = mid - 1 | ||||
| } | ||||
| } | ||||
|
|
||||
| return left | ||||
| } | ||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove TODO comment.
The TODO comment should be removed as the function is now implemented.
Apply this diff:
- // TODO: Implement this function📝 Committable suggestion
🤖 Prompt for AI Agents