Asteroid Collision Problem Description

Given an array/list ASTEROIDS representing asteroids aligned in a row, each element's absolute value identifies the asteroid's size, while its sign indicates the direction (+ve means moving right, -ve means moving left). An asteroid with a value of 0 is massless and moves to the right. Your task is to determine the state of the asteroids after all possible collisions have occurred.

Example:

Input:
ASTEROIDS = {3, -2, 4}
Output:
{3, 4}
Explanation:

The first asteroid will destroy the second asteroid. Thus, the remaining asteroids are {3, 4}.

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 10^4
  • -10^5 <= ASTEROIDS[i] <= 10^5
  • Where ASTEROIDS[i] denotes the i-th asteroid.
  • Time Limit: 1 sec
Input:
The first line contains an integer ‘T’ for the number of test cases.
The second line of each test case contains an integer ‘N’ for the number of elements.
The third line contains ‘N’ space-separated integers denoting the array ASTEROIDS.
Output:
For each test case, print all remaining asteroids separated by a space.
Note:
Implementation is needed for the collision logic; printing is handled elsewhere.
AnswerBot
4d

Determine the state of asteroids after collisions occur.

  • Iterate through the array of asteroids and simulate collisions between adjacent asteroids.

  • Use a stack to keep track of remaining asteroids after...read more

Help your peers!
Add answer anonymously...
Dunzo Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter