-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrMinDistance.go
50 lines (41 loc) · 971 Bytes
/
arrMinDistance.go
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
46
47
48
49
50
package main
import (
"fmt"
"math"
)
func testArrayMinDistance() {
arr1 := []int{3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}
x1 := 3
y1 := 6
arrayMinDistance(arr1, x1, y1)
arr2 := []int{2, 5, 3, 5, 4, 4, 2, 3}
x2 := 3
y2 := 2
arrayMinDistance(arr2, x2, y2)
}
// Given an unsorted array and two numbers x and y, find the minimum distance
// between x and y in the array
func arrayMinDistance(inputArray []int, x, y int) (minDistance int) {
for i := 0; i < len(inputArray); i++ {
if inputArray[i] == x {
var xPos, yPos int
xPos = i
for j := 0; j < len(inputArray); j++ {
if inputArray[j] == y {
yPos = j
localMinDistance := int(math.Abs(float64(xPos) - float64(yPos)))
if minDistance == 0 {
minDistance = localMinDistance
break
}
if localMinDistance < minDistance {
minDistance = localMinDistance
break
}
}
}
}
}
fmt.Printf("Array minimum distance is %d\n", minDistance)
return
}