Skip to content

Commit 0871993

Browse files
committed
add solution for product of array except self problem
1 parent fe804f2 commit 0871993

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package youngDaLee
2+
3+
func productExceptSelf(nums []int) []int {
4+
n := len(nums)
5+
result := make([]int, n)
6+
7+
// Calculate left products
8+
leftProduct := 1
9+
for i := 0; i < n; i++ {
10+
result[i] = leftProduct
11+
leftProduct *= nums[i]
12+
}
13+
14+
// Calculate right products and combine with left products
15+
rightProduct := 1
16+
for i := n - 1; i >= 0; i-- {
17+
result[i] *= rightProduct
18+
rightProduct *= nums[i]
19+
}
20+
return result
21+
}

0 commit comments

Comments
 (0)