Skip to content

Commit

Permalink
Merge pull request #1928 from Natan7/next_greater_element
Browse files Browse the repository at this point in the history
Next Greater Element
  • Loading branch information
shoaibrayeen authored Oct 4, 2023
2 parents 74fea03 + 01e41b4 commit d868bcc
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit d868bcc

Please sign in to comment.