Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions challenge-21/submissions/yz4230/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 {
left, right := 0, len(arr)
for left < right {
mid := (right - left) / 2 + left
midv := arr[mid]
if target < midv {
right = mid
} else if midv < target {
left = mid + 1
} else {
return mid
}
}
return -1
}
Comment on lines +28 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Inconsistent interval convention with recursive variant.

The iterative BinarySearch uses a half-open interval [left, right) (initialized with right := len(arr)), while BinarySearchRecursive uses a closed interval [left, right] (called with len(arr)-1). Both implementations are correct individually, but the inconsistency can confuse maintainers.

Consider standardizing on one convention across both functions for better code consistency.

🤖 Prompt for AI Agents
challenge-21/submissions/yz4230/solution-template.go lines 28-42: the iterative
BinarySearch uses a half-open interval [left,right) (right := len(arr)) while
the recursive variant uses a closed interval [left,right] (invoked with
len(arr)-1), causing inconsistency; pick one convention and make both functions
match — either change the iterative function to use closed interval (initialize
right := len(arr)-1, adjust loop condition to left <= right and update pointers
accordingly) or change the recursive variant to use half-open bounds (accept
right as len(arr) and treat right as exclusive in the recursion), and update all
comparisons/indices and base cases to follow the chosen convention consistently.


// 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 {
if left > right {
return -1
}

mid := (right - left) / 2 + left
midv := arr[mid]
if target < midv {
return BinarySearchRecursive(arr, target, left, mid-1)
} else if midv < target {
return BinarySearchRecursive(arr, target, mid+1, right)
} else {
return mid
}
}

// 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 {
left, right := 0, len(arr)
for left < right {
mid := (right - left) / 2 + left
midv := arr[mid]
if target < midv {
right = mid
} else if midv < target {
left = mid + 1
} else {
return mid
}
}
return left
}
Loading