Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

91. Asteroid Collision

MediumDay 26 of 60Stack & Queue

An array asteroids lists asteroids in a row. The absolute value is the size, and the sign is the direction: positive moves right, negative moves left. All move at the same speed. When a right mover meets a left mover, the smaller one explodes; if they are the same size, both explode. Two asteroids moving the same way never meet. Return the asteroids that survive, in order.

Example 1
Input: asteroids = [5,10,-5]
Output: [5,10]
10 and -5 collide and 10 survives. 5 and 10 move the same way.
Example 2
Input: asteroids = [10,2,-5]
Output: [10]
-5 destroys 2, then 10 destroys -5.
Constraints
  • 2 <= asteroids.length <= 10^4
  • -1000 <= asteroids[i] <= 1000
  • asteroids[i] != 0
Hints
Frequently asked at: Amazon, Uber, LinkedIn, Goldman Sachs
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min