Asteroid Collision

MediumAcc. 86.2%
+35 XP 15

Orbital Momentum

Negative moves left, positive moves right. When they meet, the smaller one explodes.

The Assignment

Your function receives asteroids.

  1. Use a stack to track moving asteroids.
  2. If current is negative and top of stack is positive:
    • Compare magnitudes.
    • Pop if stack asteroid is smaller.
    • If equal, pop and stop current.
    • If stack is larger, stop current.
  3. Print the final state of surviving asteroids.

01EXAMPLE 1

Input[5, 10, -5]
Output5 10

Explanation: 10 destroys -5.

Constraints

  • Logic for multiple collisions.
Data StructuresStack
JavaScript
Loading...
1 Hidden

Input Arguments

asteroids[5,10,-5]

Expected Output

5
10

Click RUN to test your solution