C++ solution 2Methods
In Method 1 TC: O(NlogN)\n1) We sort the vector, and take long long sum(otherwise it will overflow) to store updated mass.\n2) Now, traverse sorted vector and k
In Method 1 TC: O(NlogN)\n1) We sort the vector, and take long long sum(otherwise it will overflow) to store updated mass.\n2) Now, traverse sorted vector and keep on updating sum(current mass)\n3) If an asteroid with mass > sum comes we return false\n4) else we return true at the end of traversal\n\nMethod 2 TC: O(N)\n1) We take a count vector of size as per asteroid[i], as its index will represent an asteroid\'s mass\n2) In 1 traversal we find max and min asteroid maxx respectively, and we will also update count of asteroid\'s with mass of count\'s(vector\'s) index\n3) In 2 traversal we will use preFix Sum concept, and when we will find such an asteroid whose mass is > than current cummulative masses of all smaller asteroids we return false\n4) Otherwise we return true at the end\n\nMethod 1\n```\nclass Solution {\npublic:\n    bool asteroidsDestroyed(int mass, vector<int>& asteroids) {\n        sort(asteroids.begin(),asteroids.end());\n        int n=asteroids.size();\n        long long sum = mass;\n        for(int i=0;i<n;i++){\n            if(sum>=asteroids[i]){\n                sum += asteroids[i];\n            }\n            else{\n                return false;\n            }\n        }\n        return true;\n    }\n};\n```\nMethod 2\n```\nclass Solution {\npublic:\n    bool asteroidsDestroyed(int mass, vector<int>& asteroids) {\n        vector<int> count(100001,0);\n        int n=asteroids.size(),mn=INT_MAX,mx=INT_MIN;\n        for(auto &it: asteroids){\n            mn=min(mn,it);\n            mx=max(mx,it);\n            count[it]++;\n        }\n        long long current=mass;\n        for(int i=mn;i<=mx;i++){\n            if(current<i && count[i]!=0){\n                return false;\n            }\n            current += (long long)i*count[i];\n        }\n        return true;\n    }\n};
['Sorting', 'Prefix Sum']