Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
* [Kth Largest Element](data_structures/arrays/kth_largest_element.py)
* [Median Two Array](data_structures/arrays/median_two_array.py)
* [Monotonic Array](data_structures/arrays/monotonic_array.py)
* [Next Greater Element](data_structures/arrays/next_greater_element.py)
* [Pairs With Given Sum](data_structures/arrays/pairs_with_given_sum.py)
* [Permutations](data_structures/arrays/permutations.py)
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
Expand Down Expand Up @@ -986,6 +987,7 @@
* [Doppler Frequency](physics/doppler_frequency.py)
* [Escape Velocity](physics/escape_velocity.py)
* [Grahams Law](physics/grahams_law.py)
* [Hamiltonian](physics/hamiltonian.py)
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
* [Hubble Parameter](physics/hubble_parameter.py)
* [Ideal Gas Law](physics/ideal_gas_law.py)
Expand Down
21 changes: 21 additions & 0 deletions data_structures/arrays/next_greater_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Program to find the Next Greater Element (NGE) for each element in the array

arr = [4, 5, 2, 25, 7, 8]
n = len(arr)

print("Original array:", arr)

# List to store results, default is -1 for elements with no greater element
nge = [-1] * n

# Outer loop for each element
for i in range(n):
# Inner loop to find the next greater element
for j in range(i + 1, n):
if arr[j] > arr[i]:
nge[i] = arr[j]
break # Stop once the next greater is found

# Print result
for i in range(n):
print(f"Next Greater Element for {arr[i]} is {nge[i]}")