-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
Data Structure/Array Or Vector/Next Greater Element/SolutionByNatan7.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |