Last Stone Weight

Difficulty Level Easy
Frequently asked in Amazon
Greedy HeapViews 4692

Last Stone Weight is a problem in which we have a set of stones having some positive weights. Now we perform a task on it till we left 1 stone or no stone. We always pick two stones having the highest weight_value and smash them together. Let’s suppose the weight of stones is a,b, and a<=b. The result of the smash is:

  • If a == b then both the stones are totally destroyed.
  • If a !=b then the stone having weight=a is totally destroyed and the stone having weight=b has new weight b-a.

In the end, there is at most 1 stone left. Print the weight of this stone or if no stone left to print 0.

Input Format

First-line containing an integer value N describing the number of stones.

Second-line containing the weight of these stones such that w[i] denotes the weight of i-th stone.

Output Format

Print in a single line the weight of the left stone. If no stone left then print 0.


  • 1<=N<=10^5
  • 1<=weight[i]<=10^9
2 7 4 1 8 1


First, we pick two heaviest stones those are 7,8. Now we smash both of them and after smash stone with weight=1 is left. Now we again pick two heaviest stones in the set {2, 4, 1, 1, 1}. Here we got 2,4. Now after smashing them we left stone with weight=2. Again choose two heaviest stones from set {1, 1, 1, 2}. Here we got 1,2. Now smash them in and we got final stone with weight=1. Again we remain only 3 stones each of weight=1. Choose two stones from them and smash. Then both stones totally destroyed. Now we left only 1 stone then print the weight of that stone. Our left stone with weight=1 so we print 1.


Step:1 Push all the stones into priority_queue such that top of queue contain heaviest weight.
Step:2 While size of priority_queue is greater than 1 then:
             a) pop value from top of priority_queue and store in variable b.
             b) pop one more value and store in variable a.
             c) perform smash such that:
                    if a!=b then:
Step:3 If size of priority queue is 0 then:
          Print 0.
          Print size of the stone which is left in the priority_queue.


/*C++ Implementation of Last Stone Weight.*/ 
using namespace std; 
int main() 
    /*input values.*/ 
    int n; 
    int weight[n];
    priority_queue<int> q;
    /*store elements in priority_queue*/
    for(int i=0;i<n;i++)
    /*smash two heaviest stones till we reach the condition atmost 1 element present.*/
        /*heaviest stone.*/
        /*second heaviest stone.*/
        /*smash result*/
    /*print result*/
    return 0; 
321 5 253 2 7 9 1045

Time Complexity

O(N*logN) where N is the number of stones given to us. We store the stones in priority queue which takes logN time to insert in the exact position of the element. Elements in the priority queue in increasing order. Means top of the stack is maximum and then second max second element from the top and so on…

Space Complexity

O(N) where N is the number of stones given to us. We use a priority queue which stores the element in increasing order.


Translate ยป