-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition_array_equal_parts_test.go
99 lines (83 loc) · 2.4 KB
/
partition_array_equal_parts_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum
package codingInterviewPrep
import (
"testing"
"github.com/stretchr/testify/assert"
)
func getSum(s []int) (sum int) {
for _, v := range s {
sum += v
}
return
}
// O(n^2) - Brute force
func canThreePartsEqualSumBruteForce(arr []int) bool {
n := len(arr)
for i := 1; i <= n-2; i++ {
for j := n - 1; j >= i+1; j-- {
sum_part1 := getSum(arr[:i])
sum_part2 := getSum(arr[i:j])
sum_part3 := getSum(arr[j:])
if sum_part1 == sum_part2 && sum_part2 == sum_part3 {
return true
}
}
}
return false
}
// O(n)
// Check if we can find two indices such that sum is 1/3 and 2/3
func canThreePartsEqualSum(arr []int) bool {
n := len(arr)
total := getSum(arr)
if total%3 != 0 {
return false
}
oneThird := total / 3
twoThird := oneThird * 2
foundFirstIndex := false
sum := 0
for i := 0; i < n-1; i++ {
sum += arr[i]
if sum == oneThird && !foundFirstIndex {
foundFirstIndex = true
continue
}
if sum == twoThird && foundFirstIndex {
return true
}
}
return false
}
func TestCanThreePartsEqualSumBruteForceReturnsTrue_1(t *testing.T) {
actual := canThreePartsEqualSumBruteForce([]int{0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1})
assert.Equal(t, true, actual)
}
func TestCanThreePartsEqualSumBruteForceReturnsTrue_2(t *testing.T) {
actual := canThreePartsEqualSumBruteForce([]int{3, 3, 6, 5, -2, 2, 5, 1, -9, 4})
assert.Equal(t, true, actual)
}
func TestCanThreePartsEqualSumBruteForceReturnsFalse(t *testing.T) {
actual := canThreePartsEqualSumBruteForce([]int{0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1})
assert.Equal(t, false, actual)
}
func TestCanThreePartsEqualSumReturnsTrue_1(t *testing.T) {
actual := canThreePartsEqualSum([]int{0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1})
assert.Equal(t, true, actual)
}
func TestCanThreePartsEqualSumReturnsTrue_2(t *testing.T) {
actual := canThreePartsEqualSum([]int{3, 3, 6, 5, -2, 2, 5, 1, -9, 4})
assert.Equal(t, true, actual)
}
func TestCanThreePartsEqualSumReturnsTrue_3(t *testing.T) {
actual := canThreePartsEqualSum([]int{1, -1, 1, -1})
assert.Equal(t, false, actual)
}
func TestCanThreePartsEqualSumReturnsTrue_4(t *testing.T) {
actual := canThreePartsEqualSum([]int{0, 0, 0, 0})
assert.Equal(t, true, actual)
}
func TestCanThreePartsEqualSumReturnsFalse(t *testing.T) {
actual := canThreePartsEqualSum([]int{0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1})
assert.Equal(t, false, actual)
}