-
-
Notifications
You must be signed in to change notification settings - Fork 414
/
Copy pathsolution.go
58 lines (53 loc) · 1005 Bytes
/
solution.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
type Pair struct {
first int
second int
}
func Min(a, b int) int {
if a < b {
return a
}
return b
}
func Max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
var input []int
for _, x := range strings.Split(os.Args[1], ",") {
x, _ := strconv.Atoi(x)
input = append(input, x)
}
//noinspection GoNilness
k := input[0]
seen := map[int]bool{}
var pairs []Pair
//noinspection GoNilness
for _, n := range input[1:] {
x := k - n
if x != n {
if seen[x] {
pairs = append(pairs, Pair{Min(x, n), Max(x, n)})
} else {
seen[n] = true
}
}
}
fmt.Print("[")
for i, pair := range pairs {
if i != 0 {
fmt.Print(", ")
}
fmt.Printf("(%d, %d)", pair.first, pair.second)
}
fmt.Print("]")
}