def calculate_products_of_all_other_elements(nums)
product_of_other_elements = Array.new(nums.length, 1)
nums.each_with_index do |_num1, i|
nums.each_with_index do |num2, j|
product_of_other_elements[i] = product_of_other_elements[i] * num2 if i != j
end
end
product_of_other_elements
end
puts(calculate_products_of_all_other_elements([1, 2, 3]))
def build_prefix_products(nums)
prefix_products = []
nums.each do |num|
prefix_products << if prefix_products.count > 0
(prefix_products.last * num)
else
num
end
end
prefix_products
end
def build_suffix_products(nums)
suffix_products = []
nums.reverse.each do |num|
suffix_products << if suffix_products.count > 0
(suffix_products.last * num)
else
num
end
end
suffix_products
end
def output(prefix_products, suffix_products, nums)
result = []
nums.reverse.each_with_index do |_num, index|
result << if index == 0
suffix_products[index + 1]
elsif index == nums.length - 1
prefix_products[index - 1]
else
(prefix_products[index - 1] * suffix_products[index + 1])
end
end
result
end
def products(nums)
prefix_products = build_prefix_products(nums)
suffix_products = build_suffix_products(nums)
suffix_products = suffix_products.reverse
output(prefix_products, suffix_products, nums)
end
puts(products([1, 2, 3]))