-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_array_partition_problem.go
More file actions
66 lines (50 loc) · 1.04 KB
/
Copy pathbinary_array_partition_problem.go
File metadata and controls
66 lines (50 loc) · 1.04 KB
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
// go 1.26
package main
import (
"fmt"
"slices"
)
// Problem statement: https://interviewing.io/questions/binary-array-partition
func main() {
Z := []int{1, 0, 1, 0, 1}
//Z := []int{1, 1, 0, 1, 1}
//Z := []int{0, 0, 0, 0}
numOfOnes := 0
for _, i := range Z {
if i == 1 {
numOfOnes += 1
}
}
if numOfOnes == 0 {
fmt.Println([]int{0, len(Z) - 1})
return
}
if numOfOnes%3 != 0 {
fmt.Println([]int{-1, -1})
return
}
totalOnes := numOfOnes / 3
ones := 0
start0 := slices.Index(Z, 1)
i := start0
fmt.Println(totalOnes)
for ones < totalOnes {
if Z[i] == 1 {
ones += 1
}
i += 1
}
fmt.Println(i, start0)
l := i - start0
start1 := slices.Index(Z[start0+l:], 1) + start0 + l
start2 := slices.Index(Z[start1+l:], 1) + start1 + l
fmt.Println(start1, start2)
l = len(Z) - start2
if slices.Equal(Z[start0:(start0+l)], Z[start1:(start1+l)]) && slices.Equal(Z[start1:(start1+l)], Z[start2:(start2+l)]) {
fmt.Println([]int{start0 + l - 1, start1 + l})
return
} else {
fmt.Println([]int{-1, -1})
return
}
}