100% Beats || Easy C++ Solution || Just see for yourself
Intuition\n Describe your first thoughts on how to solve this problem. \nsort in descending order and going through the first two elements and repeating the pro
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nsort in descending order and going through the first two elements and repeating the process.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach is very simple. Follow the following steps to understand the approach.\n\n1)Sort the stones vector in descending order.\n2)store the difference of first two numbers(stones[0] and stones[1]) in res.\n3)If res is 0 then remove stones[0] and stones[1]. If res is not equal to zero which is a positve integer, then remove the front element in the vector and replace the res value with front element in the vector. (Since we need to remove 1st two elements and push the positve integer, instead of that am removing one element and replacing the other).\n4)Repeat the steps until stones vector is having atleast 2 elements.\n5)If there is an element in the stones vector, then that is our answer else return 0.\n\n# Complexity\n- Time complexity: O(N*NLogN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int lastStoneWeight(vector<int>& stones) {\n        sort(stones.begin(), stones.end(), greater<int>());\n        int i = 0;\n        while (i<stones.size() && (i+1)<stones.size()){\n            int res = stones[i] - stones[i+1];\n            if (res != 0){\n                stones.erase(stones.begin());\n                stones[0] = res;\n                sort(stones.begin(), stones.end(), greater<int>());\n            }\n            else{\n                stones.erase(stones.begin(), stones.begin()+2);\n            }\n        }\n        if (stones.size()){\n            return stones[0];\n        }\n        return 0;\n    }\n};\n\n//                         (sorted)         (updated vector)\n//[2, 7, 4, 1, 8, 1] = [8, 7, 4, 2, 1, 1] -> [1, 4, 2, 1, 1] = [4, 2, 1, 1, 1] ->\n// [2, 1, 1, 1] -> [1, 1, 1] -> \n```
['C++']