Step 1: Push 4 onto stack
Array: [4, 5, 2, 25, 7, 8]
Stack: [4]
Result: [-1, -1, -1, -1, -1, -1]
Why: Start with first element, no previous elements to compare
Step 2: 5 is greater than top of stack (4), pop 4 and set result[0] = 5, then push 5
Array: [4, 5, 2, 25, 7, 8]
Stack: [5]
Result: [5, -1, -1, -1, -1, -1]
Why: 5 is next greater for 4, update result and keep 5 for next comparisons
Step 3: 2 is not greater than top of stack (5), push 2
Array: [4, 5, 2, 25, 7, 8]
Stack: [5, 2]
Result: [5, -1, -1, -1, -1, -1]
Why: 2 has no next greater found yet, keep it for future checks
Step 4: 25 is greater than top of stack (2), pop 2 and set result[2] = 25; 25 is also greater than 5, pop 5 and set result[1] = 25; push 25
Array: [4, 5, 2, 25, 7, 8]
Stack: [25]
Result: [5, 25, 25, -1, -1, -1]
Why: 25 is next greater for 2 and 5, update results and keep 25 for next comparisons
Step 5: 7 is not greater than top of stack (25), push 7
Array: [4, 5, 2, 25, 7, 8]
Stack: [25, 7]
Result: [5, 25, 25, -1, -1, -1]
Why: 7 has no next greater found yet, keep it for future checks
Step 6: 8 is greater than top of stack (7), pop 7 and set result[4] = 8; 8 is not greater than 25, push 8
Array: [4, 5, 2, 25, 7, 8]
Stack: [25, 8]
Result: [5, 25, 25, -1, 8, -1]
Why: 8 is next greater for 7, update result and keep 8 for next comparisons
Step 7: End of array reached, pop remaining stack elements and set their results to -1
Array: [4, 5, 2, 25, 7, 8]
Stack: []
Result: [5, 25, 25, -1, 8, -1]
Why: No next greater elements for remaining stack items
Result: Result array: [5, 25, 25, -1, 8, -1]