From 01e41b49538e4452907ae774cc55b8f06cc3b302 Mon Sep 17 00:00:00 2001 From: Natan7 Date: Sun, 1 Oct 2023 21:10:54 -0300 Subject: [PATCH] Next Greater Element --- .../Next Greater Element/SolutionByNatan7.rb | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Data Structure/Array Or Vector/Next Greater Element/SolutionByNatan7.rb diff --git a/Data Structure/Array Or Vector/Next Greater Element/SolutionByNatan7.rb b/Data Structure/Array Or Vector/Next Greater Element/SolutionByNatan7.rb new file mode 100644 index 000000000..44822fc58 --- /dev/null +++ b/Data Structure/Array Or Vector/Next Greater Element/SolutionByNatan7.rb @@ -0,0 +1,52 @@ +def get_next_greater (index, array) + next_greater = array[index].to_i + index = index + 1 + + if index >= array.size + return -1 + end + + for i in index..(array.size-1) + if next_greater < array[i].to_i + return array[i].to_i + end + end + + return -1 +end + +def next_greaters_element (array) + array_greaters = [] + + for i in 0..(array.size-1) + next_greater = array[i] + array_greaters.push(get_next_greater(i, array)) + end + + return array_greaters +end + +# Take elements number of array and array elements +print("Enter size of array: ") +number = gets.chomp.to_i + +if number <= 1 || number> 10**6 + print("Oh, invalid range!\n") + exit +end + +array = [] +print("Now, enter each element of array:\n") +i = 1 +while i<=number do + element = gets.chomp.to_i + if element <= 0 || element > 10**9 + print("Oh, invalid range of element!\nTry again!\n") + else + array.push(element) + i=i+1 + end +end + +print("Array:" + array.to_s + "\n") +print("Next greaters elements array:" + next_greaters_element(array).to_s + "\n")