🧠
Brute Force (Simulation with Nested Loops)
💡 This approach simulates collisions by repeatedly scanning the array and resolving collisions one by one. It is slow but helps understand the collision process clearly.
Intuition
Check every pair of adjacent asteroids moving towards each other and resolve their collision. Repeat until no collisions remain.
Algorithm
- Start from the beginning of the array and look for adjacent asteroids moving towards each other (right then left).
- When found, compare their sizes and remove the smaller one or both if equal.
- Repeat the scan from the start until no collisions occur in a full pass.
- Return the remaining asteroids.
💡 This algorithm is hard to see because it requires multiple passes and careful index management to simulate collisions correctly.
def asteroidCollision(asteroids):
changed = True
while changed:
changed = False
i = 0
while i < len(asteroids) - 1:
if asteroids[i] > 0 and asteroids[i+1] < 0:
if abs(asteroids[i]) == abs(asteroids[i+1]):
asteroids.pop(i+1)
asteroids.pop(i)
changed = True
i = max(i-1, 0)
elif abs(asteroids[i]) > abs(asteroids[i+1]):
asteroids.pop(i+1)
changed = True
else:
asteroids.pop(i)
changed = True
i = max(i-1, 0)
else:
i += 1
return asteroids
# Driver code
if __name__ == '__main__':
print(asteroidCollision([5,10,-5])) # Output: [5,10]
Line Notes
changed = TrueInitialize flag to track if any collision happened in the pass
while changed:Repeat until no collisions occur in a full pass
if asteroids[i] > 0 and asteroids[i+1] < 0:Check if adjacent asteroids are moving towards each other
asteroids.pop(i+1)Remove the smaller asteroid after collision
i = max(i-1, 0)Step back to re-check previous pairs after modification
import java.util.*;
public class Solution {
public static int[] asteroidCollision(int[] asteroids) {
List<Integer> list = new ArrayList<>();
for (int a : asteroids) list.add(a);
boolean changed = true;
while (changed) {
changed = false;
for (int i = 0; i < list.size() - 1; ) {
int curr = list.get(i);
int next = list.get(i + 1);
if (curr > 0 && next < 0) {
if (Math.abs(curr) == Math.abs(next)) {
list.remove(i + 1);
list.remove(i);
changed = true;
i = Math.max(i - 1, 0);
} else if (Math.abs(curr) > Math.abs(next)) {
list.remove(i + 1);
changed = true;
} else {
list.remove(i);
changed = true;
i = Math.max(i - 1, 0);
}
} else {
i++;
}
}
}
int[] res = new int[list.size()];
for (int i = 0; i < list.size(); i++) res[i] = list.get(i);
return res;
}
public static void main(String[] args) {
int[] result = asteroidCollision(new int[]{5,10,-5});
System.out.println(Arrays.toString(result)); // [5, 10]
}
}
Line Notes
boolean changed = true;Flag to track if any collision happened in the current iteration
for (int i = 0; i < list.size() - 1; )Iterate through list to find collisions
if (curr > 0 && next < 0)Check if two adjacent asteroids are moving towards each other
list.remove(i + 1);Remove smaller asteroid after collision
i = Math.max(i - 1, 0);Step back to re-check previous pairs after modification
#include <iostream>
#include <vector>
using namespace std;
vector<int> asteroidCollision(vector<int>& asteroids) {
bool changed = true;
while (changed) {
changed = false;
for (int i = 0; i < (int)asteroids.size() - 1;) {
if (asteroids[i] > 0 && asteroids[i+1] < 0) {
if (abs(asteroids[i]) == abs(asteroids[i+1])) {
asteroids.erase(asteroids.begin() + i + 1);
asteroids.erase(asteroids.begin() + i);
changed = true;
i = max(i - 1, 0);
} else if (abs(asteroids[i]) > abs(asteroids[i+1])) {
asteroids.erase(asteroids.begin() + i + 1);
changed = true;
} else {
asteroids.erase(asteroids.begin() + i);
changed = true;
i = max(i - 1, 0);
}
} else {
i++;
}
}
}
return asteroids;
}
int main() {
vector<int> asteroids = {5, 10, -5};
vector<int> result = asteroidCollision(asteroids);
for (int a : result) cout << a << " ";
cout << endl; // Output: 5 10
return 0;
}
Line Notes
bool changed = true;Flag to track if any collision happened in the current pass
for (int i = 0; i < (int)asteroids.size() - 1;)Iterate through asteroids to find collisions
if (asteroids[i] > 0 && asteroids[i+1] < 0)Check if adjacent asteroids are moving towards each other
asteroids.erase(asteroids.begin() + i + 1);Remove smaller asteroid after collision
i = max(i - 1, 0);Step back to re-check previous pairs after modification
function asteroidCollision(asteroids) {
let changed = true;
while (changed) {
changed = false;
let i = 0;
while (i < asteroids.length - 1) {
if (asteroids[i] > 0 && asteroids[i + 1] < 0) {
if (Math.abs(asteroids[i]) === Math.abs(asteroids[i + 1])) {
asteroids.splice(i, 2);
changed = true;
i = Math.max(i - 1, 0);
} else if (Math.abs(asteroids[i]) > Math.abs(asteroids[i + 1])) {
asteroids.splice(i + 1, 1);
changed = true;
} else {
asteroids.splice(i, 1);
changed = true;
i = Math.max(i - 1, 0);
}
} else {
i++;
}
}
}
return asteroids;
}
// Test
console.log(asteroidCollision([5,10,-5])); // [5, 10]
Line Notes
let changed = true;Flag to track if any collision happened in the current iteration
while (changed) {Repeat until no collisions occur in a full pass
if (asteroids[i] > 0 && asteroids[i + 1] < 0)Check if adjacent asteroids are moving towards each other
asteroids.splice(i, 2);Remove both asteroids if they are equal size
i = Math.max(i - 1, 0);Step back to re-check previous pairs after modification
Each pass can remove at most one asteroid, and each pass scans the array, so worst case O(n^2).
💡 For n=20, this means up to 400 operations, which is inefficient for large inputs.
Interview Verdict: TLE / Use only to introduce
This approach is too slow for large inputs but helps understand the problem mechanics.