-
Notifications
You must be signed in to change notification settings - Fork 376
/
corrupt_pair_test.go
67 lines (55 loc) · 1.17 KB
/
corrupt_pair_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
/*
Problem:
- Given an array containing n+1 numbers taken from the range 1 to n. One
of the numbers got duplicated which also resulted in one number going
missing. Find these numbers.
Example:
- Input: []int{3, 1, 2, 5, 2}
Output: []int{2, 4}
Approach:
- Similar to finding duplicates problem, can place each number on its correct
index.
- The one is not at its correct index is the duplicate and its index itself
is the missing number.
Cost:
- O(n) time, O(1) space.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestFindCorruptPair(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{}, []int{0, 0}},
{[]int{3, 1, 2, 5, 2}, []int{2, 4}},
{[]int{3, 1, 2, 3, 6, 4}, []int{3, 5}},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
findCorruptPair(tt.in),
)
}
}
func findCorruptPair(nums []int) []int {
i := 0
for i < len(nums) {
correctIndex := nums[i] - 1
if nums[i] != nums[correctIndex] {
common.Swap(nums, i, correctIndex)
} else {
i++
}
}
for j := 0; j < len(nums); j++ {
if nums[j] != j+1 {
return []int{nums[j], j + 1}
}
}
return []int{0, 0}
}