


Input: ‘asteroids’ = [3,-2,4]
Output: [3, 4]
Explanation: The first asteroid will destroy the second asteroid. Hence, after the collision, the state of the asteroids will be [3,4].
You don’t need to print anything. Just implement the given function.
The first line of each test case contains a single integer ‘n’ denoting the number of elements in the array/list.
The second line of each test case contains ‘n’ single space-separated integers denoting the elements of the array/list.
Return an array representing the final state of asteroids.
The idea is to use the stack to maintain the state of the asteroids. If we find an asteroid moving to the left (-ve value), let’s say at index ‘i’, it will collide with an asteroid moving to the right (+ve value) and having an index less than ‘i’. Hence, we can use a stack to keep track of asteroids moving to the right and then pop the elements from the stack as and when any asteroid gets destroyed.
The steps are as follows :