Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find the product of the elements in an Array without the element itself. [1,2,3], newArray = [6,3,2]
def product_of_elements_array(a)
i = 0
prod = 1
while i <= (a.length-1)
prod = prod * a[i]
i+=1
end
prod
end
def products_of_array(a)
i = 0
arr = []
tmp = 0
while i<= a.length-1
tmp = a[i]
a[i] = 1
arr.push(product_of_elements_array(a))
a[i] = tmp
i+=1
end
arr
end
puts products_of_array([1,2,1,0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.