diff --git a/introductory.go b/introductory.go index c6ab52d..ab0688e 100644 --- a/introductory.go +++ b/introductory.go @@ -19,3 +19,29 @@ func WeirdAlgo(n int) string { return fmt.Sprint(seq) } + +func MissingNumber(n int, numbers []int) int { + + if n != len(numbers)+1 { + return -1 + } + + present := make([]bool, n) + + for _, i := range numbers { + + if i > n { + return -2 + } + + present[i-1] = true + } + + for i, b := range present { + if !b { + return i + 1 + } + } + + return -3 +} diff --git a/introductory_test.go b/introductory_test.go index b2c87cf..a20c4fa 100644 --- a/introductory_test.go +++ b/introductory_test.go @@ -9,3 +9,8 @@ import ( func TestWeirdAlgo(t *testing.T) { assert.Equal(t, "[3 10 5 16 8 4 2 1]", WeirdAlgo(3)) } + +func TestMissingNumber(t *testing.T) { + assert.Equal(t, 4, MissingNumber(5, []int{2, 3, 1, 5})) + assert.Equal(t, 7, MissingNumber(9, []int{2, 3, 1, 5, 4, 6, 9, 8})) +}