Asteroid Collision
Practice this coding question on Coding Gurukul.
Problem statement
A row of moving particles is modeled as signed integers. Magnitude describes size and sign describes direction. Only particles moving toward one another can collide, and the smaller particle disappears on impact. Your task Simulate collisions and return the surviving particles in their original left-to-right order. Positive values move right and negative values move left; all particles have equal speed. Equal-size collisions destroy both particles. Important details A right-moving particle can collide only with a later left-moving particle. Particles moving in the same direction never meet, and a left-moving particle before a right-moving one moves away from it. One particle may survive several successive collisions. Worked example For [5, 10, -6], the -6 meets 10 and is destroyed. The survivors are 5 and 10.
Input format
The first line contains n. The next line contains n space-separated integers; for n = 0 this line may be empty.
Output format
Print the resulting values in order, separated by spaces. Print EMPTY if there are no values.
Constraints
2 <= n <= 10000; -1000 <= values <= 1000; no value is zero.
Sample input
3 5 10 -6
Sample output
5 10
Sign in to run or submit a solution in C++, Java or Python.