Skip to content

Commit 1c88a32

Browse files
committed
Fixes #1
1 parent 5628de6 commit 1c88a32

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

product_of_array_except_self.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ def product_except_self(nums):
22
n = len(nums)
33
result = [1] * n
44

5+
left_products = [1] * n
6+
right_products = [1] * n
7+
8+
for i in range(1, n):
9+
left_products[i] = left_products[i - 1] * nums[i - 1]
10+
11+
for i in range(n - 2, -1, -1):
12+
right_products[i] = right_products[i + 1] * nums[i + 1]
13+
514
for i in range(n):
6-
for j in range(n):
7-
if i != j:
8-
result[i] *= nums[j]
9-
15+
result[i] = left_products[i] * right_products[i]
16+
1017
return result
1118

1219
# Test the function with the example input

0 commit comments

Comments
 (0)