mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-02 06:20:50 +01:00
Merge pull request #64 from vbrazo/add-first-array-example
Array data structure: Get products of all other elements
This commit is contained in:
commit
9b74ae2e5b
2 changed files with 91 additions and 0 deletions
|
@ -3,6 +3,8 @@
|
||||||
* [Merkle Hellman Cryptosystem](https://github.com/TheAlgorithms/Ruby/blob/master/ciphers/merkle_hellman_cryptosystem.rb)
|
* [Merkle Hellman Cryptosystem](https://github.com/TheAlgorithms/Ruby/blob/master/ciphers/merkle_hellman_cryptosystem.rb)
|
||||||
|
|
||||||
## Data Structures
|
## 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
|
* Binary Trees
|
||||||
* [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb)
|
* [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)
|
* [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb)
|
||||||
|
|
89
data_structures/arrays/get_products_of_all_other_elements.rb
Normal file
89
data_structures/arrays/get_products_of_all_other_elements.rb
Normal 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]
|
Loading…
Reference in a new issue