Skip to content

Commit 40e39aa

Browse files
committed
Array element products except self
1 parent b4c6413 commit 40e39aa

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Function to compute the product of all elements in the list except the one at each index
2+
fn product_except_self(nums: List[Int]) -> List[Int]:
3+
# If the input list is empty, return it as is
4+
if len(nums) == 0:
5+
return nums
6+
7+
# Store the length of the input list
8+
len = len(nums)
9+
10+
# Initialize the result list with all 1s. This will store our final answer.
11+
result = List(1) * len
12+
13+
# prefix_product holds the product of all elements to the *left* of the current index
14+
prefix_product = 1
15+
for idx in range(len):
16+
# For each index, store the current prefix product
17+
result[idx] = prefix_product
18+
# Update the prefix product by multiplying it with the current number
19+
prefix_product *= nums[idx]
20+
21+
# suffix_product holds the product of all elements to the *right* of the current index
22+
suffix_product = 1
23+
# Iterate from right to left
24+
for idx in range(len - 1, -1, -1):
25+
# Multiply the result at index with the current suffix product
26+
result[idx] *= suffix_product
27+
# Update the suffix product by multiplying with current number
28+
suffix_product *= nums[idx]
29+
30+
return result
31+
32+
33+
# Entry point
34+
fn main():
35+
# Example input
36+
nums = List(1, 2, 3, 4)
37+
# Call the function to get result
38+
result = product_except_self(nums) # Output should be [24, 12, 8, 6]
39+
# Print the result
40+
print(result.__str__())

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
- [ Buy and Sell Stock 1](buy_and_sell_stock.md) ➔ Maximize profit by buying once and selling later on a future day.
66
- [ Buy and Sell Stock 2](buy_and_sell_stock_2.md) ➔ Maximize profit by buying and selling multiple times.
77
- [ Longest Substring Without Repeating Characters](longest_substr_no_char_repeats.md) ➔ Given a string s, find the length of the longest substring without duplicate characters.
8+
- [ Product of Array Except Self](product_of_array_except_self.md) ➔ Return an array where each element is the product of all others in the input array.

docs/product_of_array_except_self.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
### Function to compute the product of all elements in the list except the one at each index
2+
3+
```python
4+
fn product_except_self(nums: List[Int]) -> List[Int]:
5+
# If the input list is empty, return it as is
6+
if len(nums) == 0:
7+
return nums
8+
9+
# Store the length of the input list
10+
len = len(nums)
11+
12+
# Initialize the result list with all 1s. This will store our final answer.
13+
result = List(1) * len
14+
15+
# prefix_product holds the product of all elements to the *left* of the current index
16+
prefix_product = 1
17+
for idx in range(len):
18+
# For each index, store the current prefix product
19+
result[idx] = prefix_product
20+
# Update the prefix product by multiplying it with the current number
21+
prefix_product *= nums[idx]
22+
23+
# suffix_product holds the product of all elements to the *right* of the current index
24+
suffix_product = 1
25+
# Iterate from right to left
26+
for idx in range(len - 1, -1, -1):
27+
# Multiply the result at index with the current suffix product
28+
result[idx] *= suffix_product
29+
# Update the suffix product by multiplying with current number
30+
suffix_product *= nums[idx]
31+
32+
return result
33+
34+
35+
# Entry point
36+
fn main():
37+
# Example input
38+
nums = List(1, 2, 3, 4)
39+
# Call the function to get result
40+
result = product_except_self(nums) # Output should be [24, 12, 8, 6]
41+
# Print the result
42+
print(result.__str__())
43+
```

0 commit comments

Comments
 (0)