Merge pull request #64 from vbrazo/add-first-array-example

Array data structure: Get products of all other elements
This commit is contained in:
Vitor Oliveira 2020-12-28 14:37:11 -08:00 committed by GitHub
commit 9b74ae2e5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 0 deletions

View file

@ -3,6 +3,8 @@
* [Merkle Hellman Cryptosystem](https://github.com/TheAlgorithms/Ruby/blob/master/ciphers/merkle_hellman_cryptosystem.rb)
## Data Structures
* Arrays
* [Get products of all other elements in Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
* Binary Trees
* [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb)
* [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb)

View file

@ -0,0 +1,89 @@
# Arrays - Get Products of all other elements in Ruby
# Algorithm challenge description:
# Given an array of integers, return a new array such that
# each element at index `i` of the new array is the product of
# all the numbers in the original array except the one at `i`.
#
# 1. Brute force solution
#
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|
if (i != j)
product_of_other_elements[i] = product_of_other_elements[i] * num2
end
end
end
product_of_other_elements
end
puts(calculate_products_of_all_other_elements([1, 2, 3]))
#
# 2. O(n) time and space
# Arrays - Get Products of all other elements in Ruby
#
# Generates prefix products
def build_prefix_products(nums)
prefix_products = []
nums.each do |num|
if prefix_products.count > 0
prefix_products << (prefix_products.last * num)
else
prefix_products << num
end
end
return prefix_products
end
# Generates suffix products
def build_suffix_products(nums)
suffix_products = []
nums.reverse.each do |num|
if suffix_products.count > 0
suffix_products << (suffix_products.last * num)
else
suffix_products << num
end
end
return suffix_products
end
# Builds output
def output(prefix_products, suffix_products, nums)
result = []
nums.reverse.each_with_index do |num, index|
if index == 0
result << suffix_products[index + 1]
elsif index == nums.length - 1
result << prefix_products[index - 1]
else
result << (prefix_products[index - 1] * suffix_products[index + 1])
end
end
return result
end
# Generate result from the product of prefixes and suffixes
def products(nums)
prefix_products = build_prefix_products(nums)
suffix_products = build_suffix_products(nums)
suffix_products = suffix_products.reverse
return output(prefix_products, suffix_products, nums)
end
puts(products([1, 2, 3]))
# => [6, 3, 2]