C++ solution with image explanation | No extra space
\n\n\nclass Solution {\npublic:\n    int lastStoneWeight(vector<int>& stones) {\n        while(stones.size() > 1) {\n            sort(stones.begin(), stones.end
![image](https://assets.leetcode.com/users/images/2926ac92-fc16-4e57-ba90-57edfee68604_1649334079.7444394.jpeg)\n\n```\nclass Solution {\npublic:\n    int lastStoneWeight(vector<int>& stones) {\n        while(stones.size() > 1) {\n            sort(stones.begin(), stones.end());\n            int n = stones.size();\n            \n            if(stones[n - 2] == stones[n - 1]) {\n                stones.pop_back();\n                stones.pop_back();\n            }\n            else {\n                stones[n - 2] = stones[n - 1] - stones[n - 2];\n                stones.pop_back();\n            }\n        }\n        return stones.empty() ? 0:stones[0];\n    }\n};\n\n```\n
[]