Back to May Back to Year 2026
MEDIUM 31 May 2026 View on LeetCode

2126. Destroying Asteroids

</> Solution

/**
 * Problem: LeetCode 2126 - Destroying Asteroids
 * Difficulty: Medium
 * Language: Java
 * Author: CodeByTushu
 */
import java.util.Arrays;

class Solution {
    public boolean asteroidsDestroyed(int mass, int[] asteroids) {
        Arrays.sort(asteroids);
        long currentMass = mass;

        for (int asteroid : asteroids) {
            if (currentMass < asteroid) {
                return false;
            }
            currentMass += asteroid;
        }

        return true;
    }
}

TIME COMPLEXITY

O(N log N)

SPACE COMPLEXITY

O(1)

TOPICS

Array Greedy Sorting