-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathmissing_numbers_test.go
70 lines (59 loc) · 1.36 KB
/
missing_numbers_test.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Problem:
- Given an array containing n distinct numbers taken from the range 0 to n.
Since the array has only n numbers out of the total n+1 numbers, find the
missing number.
Example:
- Input: []int{4, 0, 3, 1}
Output: 2
Approach:
- Sort the array using the cyclic sort first.
- The one that does not have the correct index is the missing one.
Cost:
- O(n) time, O(1) space.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestMissingNumber(t *testing.T) {
tests := []struct {
in []int
expected int
}{
{[]int{}, 0},
{[]int{4, 0, 3, 1}, 2},
{[]int{8, 3, 5, 2, 4, 7, 0, 1}, 6},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
missingNumber(tt.in),
)
}
}
func missingNumber(nums []int) int {
i := 0
for i < len(nums) {
// if the current number is not at the correct index, swap it with the
// one that is at the correct index.
// note that we have to also make sure the current value is not larger
// than the length of the array because it would causes out of index
// error.
correctIndex := nums[i]
if nums[i] < len(nums) && nums[i] != nums[correctIndex] {
common.Swap(nums, i, correctIndex)
} else {
i++
}
}
// return the missing number that does not have the correct index.
for j := 0; j < len(nums); j++ {
if nums[j] != j {
return j
}
}
return len(nums)
}