From 822414f0783a4c33ff18a1d25266e98469f38799 Mon Sep 17 00:00:00 2001 From: Naresh <58628794+ortin779@users.noreply.github.com> Date: Wed, 18 Sep 2024 22:54:28 +0530 Subject: [PATCH] Create Product of Array Except Self - Leetcode 238.go Product of Array Except Self solution in golang --- ...uct of Array Except Self - Leetcode 238.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.go diff --git a/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.go b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.go new file mode 100644 index 0000000..0f2416c --- /dev/null +++ b/Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.go @@ -0,0 +1,19 @@ +package solution + +func productExceptSelf(nums []int) []int { + res := make([]int , len(nums)) + + res[0] = 1 + + for index := 1 ; index < len(nums) ; index++ { + res[index] = nums[index - 1] * res[index - 1]; + } + + suffix := 1; + + for index := len(nums) - 1 ; index >= 0 ;index-- { + res[index] *= suffix + suffix *= nums[index] + } + return res +}