Step 1: Push 4 onto stack
Stack: [4]
Next Greater Elements: [-, -, -, -, -, -]
Why: Start with first element, no next greater found yet
Step 2: 5 is greater than top of stack (4), pop 4 and set its next greater to 5; push 5
Stack: [5]
Next Greater Elements: [5, -, -, -, -, -]
Why: 5 is next greater for 4, update and continue
Step 3: 2 is not greater than top of stack (5), push 2
Stack: [5, 2]
Next Greater Elements: [5, -, -, -, -, -]
Why: 2 might find next greater later, keep it on stack
Step 4: 25 is greater than top of stack (2), pop 2 and set next greater to 25; also greater than 5, pop 5 and set next greater to 25; push 25
Stack: [25]
Next Greater Elements: [5, 25, 25, -, -, -]
Why: 25 is next greater for 2 and 5, update both
Step 5: 7 is not greater than top of stack (25), push 7
Stack: [25, 7]
Next Greater Elements: [5, 25, 25, -, -, -]
Why: 7 might find next greater later
Step 6: 8 is greater than top of stack (7), pop 7 and set next greater to 8; push 8
Stack: [25, 8]
Next Greater Elements: [5, 25, 25, -, 8, -]
Why: 8 is next greater for 7
Step 7: No more elements; pop remaining stack elements (25, 8) and set their next greater to -1
Stack: empty
Next Greater Elements: [5, 25, 25, -1, 8, -1]
Why: No next greater element for 25 and 8
Result: Next Greater Elements: [5, 25, 25, -1, 8, -1]