From d24e95ae02dd2eed13501181cb3d69b350973959 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:18:06 -0800 Subject: [PATCH 1/6] Add brute force solution --- .../get_products_of_all_other_elements.rb | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 data_structures/arrays/get_products_of_all_other_elements.rb diff --git a/data_structures/arrays/get_products_of_all_other_elements.rb b/data_structures/arrays/get_products_of_all_other_elements.rb new file mode 100644 index 0000000..8bfe4a6 --- /dev/null +++ b/data_structures/arrays/get_products_of_all_other_elements.rb @@ -0,0 +1,23 @@ +# 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) + products_other_elements = Array.new(nums.length, 1) + + nums.each_with_index do |num1, i| + nums.each_with_index do |num2, j| + if (i != j) + products_other_elements[i] = products_other_elements[i] * num2 + end + end + end + + products_other_elements +end + +puts(calculate_products_of_all_other_elements([1, 2, 3])) From ecde337d9bab5665169c161f88345e0230a25226 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:18:22 -0800 Subject: [PATCH 2/6] Add better solution - O(n) time and space --- .../get_products_of_all_other_elements.rb | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/data_structures/arrays/get_products_of_all_other_elements.rb b/data_structures/arrays/get_products_of_all_other_elements.rb index 8bfe4a6..b4a8a2f 100644 --- a/data_structures/arrays/get_products_of_all_other_elements.rb +++ b/data_structures/arrays/get_products_of_all_other_elements.rb @@ -21,3 +21,65 @@ def calculate_products_of_all_other_elements(nums) 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] From 13f9770de540fd3470623114358f6f6db99582e2 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:19:07 -0800 Subject: [PATCH 3/6] Add DIRECTORY.md entry --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..e2a7845 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) From dcfdf73a7f937e12470a585add4e6107ef942583 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:20:45 -0800 Subject: [PATCH 4/6] Separators --- data_structures/arrays/get_products_of_all_other_elements.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/arrays/get_products_of_all_other_elements.rb b/data_structures/arrays/get_products_of_all_other_elements.rb index b4a8a2f..e22dd1a 100644 --- a/data_structures/arrays/get_products_of_all_other_elements.rb +++ b/data_structures/arrays/get_products_of_all_other_elements.rb @@ -5,7 +5,9 @@ # 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) products_other_elements = Array.new(nums.length, 1) @@ -22,8 +24,10 @@ 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) From a285989e1cf2601c519bd338aab2d8b115b879b6 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:23:48 -0800 Subject: [PATCH 5/6] Rename var --- .../arrays/get_products_of_all_other_elements.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/get_products_of_all_other_elements.rb b/data_structures/arrays/get_products_of_all_other_elements.rb index e22dd1a..0e7d97d 100644 --- a/data_structures/arrays/get_products_of_all_other_elements.rb +++ b/data_structures/arrays/get_products_of_all_other_elements.rb @@ -7,19 +7,19 @@ # # 1. Brute force solution -# +# def calculate_products_of_all_other_elements(nums) - products_other_elements = Array.new(nums.length, 1) + 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) - products_other_elements[i] = products_other_elements[i] * num2 + product_of_other_elements[i] = product_of_other_elements[i] * num2 end end end - products_other_elements + product_of_other_elements end puts(calculate_products_of_all_other_elements([1, 2, 3])) From 6a64ce0c5ac381f79a7243b62341e90a05a0e7ca Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 19 Dec 2020 20:24:18 -0800 Subject: [PATCH 6/6] Update DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index e2a7845..d0f0b0a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -4,7 +4,7 @@ ## 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) + * [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)