Skip to content

Commit f3c5714

Browse files
rodney-bbumpow
andauthored
Add solution for Challenge 21 by rodney-b (#707)
* Add solution for Challenge 6 by rodney-b * Add solution for Challenge 21 by rodney-b * Implemented improvements suggested by CodeRabbit * Refactored for consistency --------- Co-authored-by: Rodney B <[email protected]>
1 parent b153ab8 commit f3c5714

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
// Example sorted array for testing
9+
arr := []int{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
10+
11+
// Test binary search
12+
target := 7
13+
index := BinarySearch(arr, target)
14+
fmt.Printf("BinarySearch: %d found at index %d\n", target, index)
15+
16+
// Test recursive binary search
17+
recursiveIndex := BinarySearchRecursive(arr, target, 0, len(arr)-1)
18+
fmt.Printf("BinarySearchRecursive: %d found at index %d\n", target, recursiveIndex)
19+
20+
// Test find insert position
21+
insertTarget := 8
22+
insertPos := FindInsertPosition(arr, insertTarget)
23+
fmt.Printf("FindInsertPosition: %d should be inserted at index %d\n", insertTarget, insertPos)
24+
}
25+
26+
// BinarySearch performs a standard binary search to find the target in the sorted array.
27+
// Returns the index of the target if found, or -1 if not found.
28+
func BinarySearch(arr []int, target int) int {
29+
arrLen := len(arr)
30+
left := 0
31+
right := arrLen - 1
32+
33+
for left <= right {
34+
mid := left + (right-left)/2 // avoids potential (left + right) overflow
35+
// mid := int(uint(left+right) >> 1) // from Go's standard library
36+
val := arr[mid]
37+
38+
switch {
39+
case target < val:
40+
right = mid - 1
41+
case target > val:
42+
left = mid + 1
43+
default:
44+
return mid
45+
}
46+
}
47+
48+
return -1
49+
}
50+
51+
// BinarySearchRecursive performs binary search using recursion.
52+
// Returns the index of the target if found, or -1 if not found.
53+
func BinarySearchRecursive(arr []int, target int, left int, right int) int {
54+
if left > right {
55+
return -1
56+
}
57+
58+
mid := left + (right-left)/2
59+
val := arr[mid]
60+
61+
switch {
62+
case target < val:
63+
return BinarySearchRecursive(arr, target, left, mid-1)
64+
case target > val:
65+
return BinarySearchRecursive(arr, target, mid+1, right)
66+
default:
67+
return mid
68+
}
69+
}
70+
71+
// FindInsertPosition returns the index where the target should be inserted
72+
// to maintain the sorted order of the array.
73+
func FindInsertPosition(arr []int, target int) int {
74+
arrLen := len(arr)
75+
if arrLen == 0 {
76+
return 0
77+
}
78+
79+
left := 0
80+
right := arrLen - 1
81+
82+
for left <= right {
83+
mid := left + (right-left)/2
84+
val := arr[mid]
85+
86+
switch {
87+
case target < val:
88+
right = mid - 1
89+
case target > val:
90+
left = mid + 1
91+
default:
92+
return mid // Insert before existing element
93+
}
94+
}
95+
96+
return left
97+
}

0 commit comments

Comments
 (0)