-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_product.go
More file actions
44 lines (37 loc) · 901 Bytes
/
array_product.go
File metadata and controls
44 lines (37 loc) · 901 Bytes
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
package arrayProduct
// given an array of integers return an array of products of all elements in that array,
// excluding the value at that index
// input [2,4,3,5] output // [60,40, 20, 24]
//if array is empty return empty array
// numbers can be negative or positive
// product will never hit integer overflow (max/min)
func arrayProduct(input []int) []int{
if len(input) == 0 {
return []int(nil)
}
var result []int
temp := 1
var zeroTracker int
for _, v := range input {
if v == 0 {
zeroTracker ++
}
if v != 0 {
temp = temp * v
}
}
var product int
for _, v := range input {
if zeroTracker > 1{
result = append(result, 0)
} else if v == 0 && zeroTracker == 1 {
result = append(result, temp)
} else if v != 0 && zeroTracker >= 1{
result = append(result, 0)
} else {
product = temp / v
result = append(result, product)
}
}
return result
}