Skip to content

Commit

Permalink
Merge pull request neetcode-gh#886 from therollingambit/238-product-o…
Browse files Browse the repository at this point in the history
…f-array-except-self

adding golang solution for 238. product of array except self
  • Loading branch information
Ahmad-A0 authored Aug 22, 2022
2 parents 0db5850 + f682c2b commit 4ee7e69
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions go/238-Product-Of-Array-Except-Self.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func productExceptSelf(nums []int) []int {
res := make([]int, len(nums))
for i := 0; i < len(res); i++ {
res[i] = 1
}

prefix := 1
for i := 0; i < len(nums); i++ {
res[i] = prefix
prefix *= nums[i]
}

postfix := 1
for i := len(nums) - 1; i >= 0; i-- {
res[i] *= postfix
postfix *= nums[i]
}

return res
}

0 comments on commit 4ee7e69

Please sign in to comment.