Skip to content

Commit f1b3500

Browse files
committed
1 parent 17599ca commit f1b3500

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

go/0658-find-k-closest-elements.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func findClosestElements(arr []int, k int, x int) []int {
2+
l, r := 0, len(arr)-k
3+
4+
for l < r {
5+
m := (l + r) / 2
6+
if x-arr[m] > arr[m+k]-x {
7+
l = m + 1
8+
} else {
9+
r = m
10+
}
11+
}
12+
return arr[l : l+k]
13+
}

0 commit comments

Comments
 (0)