-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_02.go
More file actions
43 lines (37 loc) · 999 Bytes
/
problem_02.go
File metadata and controls
43 lines (37 loc) · 999 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
package main
/*
Task: Given an array of integers, return a new array such that each element at index
i of the new array is the product of all the numbers in the original array except the one at i.
**/
//solution without division : no need of special treatment for the case of an element being 0
//downside: runntime O(nˆ2)
func ProductWithoutDivision(array []int) []int {
result := make([]int, len(array))
/**
This first loop checks for a zero, in this case every other element with index != zeroIndex
will be zero and we get a linear runtime
*/
for index, elem := range array {
if elem == 0 {
zeroIndexValue := 1
for innerIndex, innerElem := range array {
if innerIndex != index {
zeroIndexValue *= innerElem
}
}
result[index] = zeroIndexValue
return result
}
}
for index := range array {
tmp := 1
for innerIndex, elem := range array {
if innerIndex != index {
tmp *= elem
}
}
result[index] = tmp
tmp = 1
}
return result
}