Asteroid Collision

Try to solve the Asteroid Collision problem.

Statement

We are provided an array of integers, asteroids, representing asteroids in a row. The absolute integer value of each asteroid represents its size, and the sign represents its direction. A positive sign signifies that the asteroid is moving toward the right direction, while a negative sign indicates it is moving toward the left. All asteroids move at the same speed.

We need to determine the final state of the asteroids after all the collisions have occurred. When two asteroids of different sizes collide, the smaller one will be destroyed. If both asteroids are of the same size, both will be destroyed.

Note: Asteroids moving in the same direction will never collide.

Constraints:

  • 2≤2 \leq asteroids.length ≤103\leq 10^3

  • −103≤-10^3 \leq asteroids[i] ≤103\leq 10^3

  • asteroids[i] ≠0\neq 0

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.