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 next greater found yet
Step 2: 5 is greater than top of stack (4), so 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 checks
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 waits for a bigger number ahead
Step 4: 25 is greater than top of stack (2), pop 2 and set result[2] = 25; 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 checks
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 waits for a bigger number ahead
Step 6: 8 is greater than top of stack (7), pop 7 and set result[4] = 8; 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 checks
Step 7: End of array reached; pop remaining stack elements (25 and 8) and set their results to -1
Array: [4, 5, 2, 25, 7, 8]
Stack: empty
Result: [5, 25, 25, -1, 8, -1]
Why: No next greater elements for 25 and 8, so result remains -1
Result: Result array: [5, 25, 25, -1, 8, -1]