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.
- Use a stack to track moving asteroids.
- 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.
- Print the final state of surviving asteroids.
01EXAMPLE 1
Input
[5, 10, -5]Output
5
10Explanation: 10 destroys -5.
Constraints
- Logic for multiple collisions.
Data StructuresStack
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
asteroids[5,10,-5]
Expected Output
5 10
Click RUN to test your solution