Asteroid Collision

We are given an array asteroids of integers representing asteroids in a row. The indices of the asteroids in the array represent their relative positions in space.

For each asteroid:

  • The absolute value represents its size.
  • The sign represents its direction, where positive means moving right and negative means moving left.
  • Each asteroid moves 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 will explode. Two asteroids moving in the same direction will never meet.

Example 1
Inputasteroids = [5,10,-5]
Output[5,10]
The 10 and -5 collide resulting in 10, while the 5 and 10 never collide.
Example 2
Inputasteroids = [8,-8]
Output[]
The 8 and -8 collide exploding each other.

Constraints

  • 2 <= asteroids.length <= 10^4
  • -1000 <= asteroids[i] <= 1000
  • asteroids[i] != 0

Asked at 33 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate