 All Problems
Asteroid Collision
medium
stack
array
amazon
google
facebook

We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive = right, negative = left). All asteroids move at the same speed.

Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both explode. Two asteroids moving in the same direction will never meet.

Example 1:

Input: 5 10 -5
Output: 5 10

Example 2:

Input: 8 -8
Output: (empty)

Example 3:

Input: 10 2 -5
Output: 10

Input format: Single line of space-separated integers.

Output format: Surviving asteroids, space-separated.

Run to check your code against the sample cases, or submit to run every case