Problem title
Difficulty
Avg time to solve

Find the Good Matrix
Moderate
30 mins
Point to Greatest Value Node
Easy
10 mins
Check if the Word is present in Sentence or not
Easy
15 mins
Kth smallest node in BST
Moderate
15 mins
Closest Binary Search Tree Value
Easy
15 mins
Add One to Linked List
Easy
10 mins
Exactly One Child
Easy
15 mins
Predecessor and Successor In BST
Moderate
25 mins
Pair sum in a BST
Easy
15 mins
Sum Of K Smallest Elements In BST
Easy
15 mins
8

Last Stone Weight

Difficulty: EASY
Contributed By
Avg. time to solve
27 min

Problem Statement

We have a collection of 'N' stones, each stone has a positive integer weight.

On each turn, we choose the two heaviest stones and smash them together. Suppose the stones have weights 'x' and 'y' with 'x' <= 'y'. The result of this smash will be:

1. If 'x' == 'y', both stones are totally destroyed;

2. If 'x' != 'y', the stone of weight 'x' is totally destroyed, and the stone of weight 'y' has a new weight equal to 'y - x'.

In the end, there is at most 1 stone left. Return the weight of this stone or 0 if there are no stones left.

Input format:
The first line of input contains the integer 'N', representing the total number of stones.

The second line of input contains 'N' single space-separated integers, representing the weights of the stones.
Output Format:
The only output line prints the weight of the last stone, if it exists, 0 otherwise.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given functions.
Constraints :
1 <= N <= 10^5
1 <= W <= 10^6

Time Limit: 1 sec
Sample Input 1:
1
10
Sample Output 1:
10
Explanation For Sample Input 1:
There is Only one stone so the weight of the last stone is 10
Sample Input 2:
3
1 9 5
Sample Output 2:
3 
Reset Code
Full screen
copy-code
Console