Skip to content

Commit 4fddd94

Browse files
committed
Smallest Common Number Algorithm
1 parent a257713 commit 4fddd94

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

arrays/smallestCommonNumber.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@ Given 3 sorted arrays, find the smallest number among the three that is common t
33
else return -1
44
*/
55

6-
package main()
6+
package main
77

88
import ("fmt"
9-
"AlgorithmsGolang/arrays/binarySearch"
109
)
1110

12-
func smallestCommonNumber() {
11+
func smallestCommonNumber(arr1,arr2,arr3 []int) int {
12+
//Use an iterators for each of the arrays
13+
i := 0
14+
j := 0
15+
k := 0
1316

17+
for i<len(arr1) && j<len(arr2) && k<len(arr3) {
18+
//We have reached our solution
19+
if arr1[i] == arr2[j] && arr2[j] == arr3[k] {
20+
fmt.Println("Smallest Number Found:",arr1[i])
21+
return arr1[i]
22+
}
23+
if arr1[i] <= arr2[j] && arr1[i] <= arr3[k] {i++
24+
} else if arr2[j] <= arr1[i] && arr2[j] <= arr3[k] {j++
25+
} else {k++}
26+
}
27+
fmt.Println("Smallest Common Number not found")
28+
return -1
1429
}
1530

16-
1731
func main() {
18-
32+
arr1 := []int{6,7,10,25,30,50,63,64}
33+
arr2 := []int{-1,4,5,8,50}
34+
arr3 := []int{1,3,4,7,10,25,50,100}
35+
smallestCommonNumber(arr1,arr2,arr3)
1936
}

0 commit comments

Comments
 (0)