forked from RezaSi/go-interview-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution-template.go
More file actions
45 lines (37 loc) · 1.36 KB
/
solution-template.go
File metadata and controls
45 lines (37 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
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
return -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
return -1
}