Last Stone Weight Problem Explanation
Given a collection of stones, each having a positive integer weight, perform the following operation: On each turn, select the two heaviest stones and smash them together. Assume the stones have weights 'x' and 'y' where 'x' <= 'y'. The outcomes of smashes are:
- If 'x' == 'y', both stones are destroyed.
- If 'x' != 'y', the stone with weight 'x' is destroyed, and the stone with weight 'y' is updated to 'y - x'.
Continue this process until at most one stone remains. Return the weight of the last stone, or 0 if no stones are left.
Input:
First line: Integer 'N', the number of stones.
Second line: 'N' space-separated integers representing the weights of the stones.
Output:
Print the weight of the final stone, or 0 if no stones remain.
Example:
Input:
N = 6
Weights = [2, 7, 4, 1, 8, 1]
Output:
1
Constraints:
1 <= N <= 10^5
1 <= W[i] <= 10^6
- Time Limit: 1 sec
Note:
Implement the function to solve the problem. No need for explicit printing or input reading as it's handled internally.

AnswerBot
1y
This question is about finding the weight of the last stone after repeatedly smashing the two heaviest stones together.
Sort the array of stone weights in descending order.
Repeatedly smash the two heav...read more
Help your peers!
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

