From ce0d1863a292cb69ab7ab01b4145e8e462058f29 Mon Sep 17 00:00:00 2001
From: Vitor Oliveira <vbrazo@gmail.com>
Date: Sat, 10 Apr 2021 10:38:43 -0700
Subject: [PATCH 1/5] Next greater element

---
 .../arrays/next_greater_element.rb            | 55 +++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 data_structures/arrays/next_greater_element.rb

diff --git a/data_structures/arrays/next_greater_element.rb b/data_structures/arrays/next_greater_element.rb
new file mode 100644
index 0000000..fc71766
--- /dev/null
+++ b/data_structures/arrays/next_greater_element.rb
@@ -0,0 +1,55 @@
+# You are given two integer arrays nums1 and nums2 both of unique elements, where nums1 is a subset of nums2.
+#
+# Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
+#
+# The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, return -1 for this number.
+
+# Example 1:
+#
+# Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
+# Output: [-1,3,-1]
+#
+# Explanation:
+# For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
+# For number 1 in the first array, the next greater number for it in the second array is 3.
+# For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
+#
+# Example 2:
+#
+# Input: nums1 = [2,4], nums2 = [1,2,3,4]
+# Output: [3,-1]
+# 
+# Explanation:
+# For number 2 in the first array, the next greater number for it in the second array is 3.
+# For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
+
+# @param {Integer[]} nums1
+# @param {Integer[]} nums2
+# @return {Integer[]}
+def next_greater_element(nums1, nums2)
+  nums1.each_with_index do |value, pointer1|
+    max = 0
+    pos_nums2 = nums2.find_index(value)
+
+    nums2[pos_nums2..nums2.count].each do |value|
+      if value > nums1[pointer1]
+        max = value
+        break
+      end
+    end
+
+    nums1[pointer1] = (nums1[pointer1] < max ? max : -1)
+  end
+
+  nums1
+end
+
+nums1 = [4, 1, 2]
+nums2 = [1, 3, 4, 2]
+print next_greater_element(nums1, nums2)
+# Output: [-1,3,-1]
+
+nums1 = [2, 4]
+nums2 = [1, 2, 3, 4]
+print next_greater_element(nums1, nums2)
+# Output: [3,-1]

From 1c9a26f389a5d62b60a10a7879c0eed848ec8c0f Mon Sep 17 00:00:00 2001
From: Vitor Oliveira <vbrazo@gmail.com>
Date: Sat, 10 Apr 2021 10:41:01 -0700
Subject: [PATCH 2/5] Minor changes

---
 data_structures/arrays/next_greater_element.rb | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/data_structures/arrays/next_greater_element.rb b/data_structures/arrays/next_greater_element.rb
index fc71766..0790ab1 100644
--- a/data_structures/arrays/next_greater_element.rb
+++ b/data_structures/arrays/next_greater_element.rb
@@ -18,7 +18,7 @@
 #
 # Input: nums1 = [2,4], nums2 = [1,2,3,4]
 # Output: [3,-1]
-# 
+#
 # Explanation:
 # For number 2 in the first array, the next greater number for it in the second array is 3.
 # For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
@@ -27,18 +27,18 @@
 # @param {Integer[]} nums2
 # @return {Integer[]}
 def next_greater_element(nums1, nums2)
-  nums1.each_with_index do |value, pointer1|
+  nums1.each_with_index do |nums1_value, pointer1|
     max = 0
-    pos_nums2 = nums2.find_index(value)
+    pos_nums2 = nums2.find_index(nums1_value)
 
-    nums2[pos_nums2..nums2.count].each do |value|
-      if value > nums1[pointer1]
-        max = value
+    nums2[pos_nums2..nums2.count].each do |nums2_value|
+      if nums2_value > nums1_value
+        max = nums2_value
         break
       end
     end
 
-    nums1[pointer1] = (nums1[pointer1] < max ? max : -1)
+    nums1[pointer1] = (nums1_value < max ? max : -1)
   end
 
   nums1

From 4e0d914d63c1eb4c4ea2eb436325b3e2992623cf Mon Sep 17 00:00:00 2001
From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Date: Sat, 10 Apr 2021 17:41:17 +0000
Subject: [PATCH 3/5] updating DIRECTORY.md

---
 DIRECTORY.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/DIRECTORY.md b/DIRECTORY.md
index c94db91..763e907 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -18,6 +18,7 @@
     * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb)
     * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb)
     * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
+    * [Next Greater Element](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/next_greater_element.rb)
     * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb)
     * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer_wealth.rb)
     * [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.rb)

From 4d9d7ca1cdad47591163d384c8de5f891f23c2a8 Mon Sep 17 00:00:00 2001
From: Vitor Oliveira <vbrazo@gmail.com>
Date: Sat, 10 Apr 2021 10:46:55 -0700
Subject: [PATCH 4/5] add complexity analysis

---
 data_structures/arrays/next_greater_element.rb | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/data_structures/arrays/next_greater_element.rb b/data_structures/arrays/next_greater_element.rb
index 0790ab1..ffe957e 100644
--- a/data_structures/arrays/next_greater_element.rb
+++ b/data_structures/arrays/next_greater_element.rb
@@ -23,6 +23,15 @@
 # For number 2 in the first array, the next greater number for it in the second array is 3.
 # For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
 
+# 
+# Approach: Brute Force
+# 
+
+# Complexity Analysis
+# 
+# Time complexity : O(m*n). The complete nums1 array (of size n) needs to be scanned for all the m elements of nums2 in the worst case.
+# Space complexity : O(1). No additional space since we're swapping elements in nums1 and returning the input array.
+
 # @param {Integer[]} nums1
 # @param {Integer[]} nums2
 # @return {Integer[]}

From 20d5ea3b6d419bd3b9fc62f07c3863607ff1c167 Mon Sep 17 00:00:00 2001
From: Vitor Oliveira <vbrazo@gmail.com>
Date: Sat, 10 Apr 2021 10:49:41 -0700
Subject: [PATCH 5/5] Update next_greater_element.rb

---
 data_structures/arrays/next_greater_element.rb | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/data_structures/arrays/next_greater_element.rb b/data_structures/arrays/next_greater_element.rb
index ffe957e..92b3344 100644
--- a/data_structures/arrays/next_greater_element.rb
+++ b/data_structures/arrays/next_greater_element.rb
@@ -29,8 +29,8 @@
 
 # Complexity Analysis
 # 
-# Time complexity : O(m*n). The complete nums1 array (of size n) needs to be scanned for all the m elements of nums2 in the worst case.
-# Space complexity : O(1). No additional space since we're swapping elements in nums1 and returning the input array.
+# Time complexity: O(m*n). The complete nums1 array (of size n) needs to be scanned for all the m elements of nums2 in the worst case.
+# Space complexity: O(1). No additional space since we're swapping elements in nums1 and returning the input array.
 
 # @param {Integer[]} nums1
 # @param {Integer[]} nums2