Last Stone Weight Problem

Last Stone Weight Problem — ExecCode Easy DSA Practice

Solve the Last Stone Weight problem on ExecCode. Free online easy DSA practice in Arrays - Logic Building. Write and run code in Java, C++, Python — no signup required to run.

Problem description

You are given an array of integers stones where stones[i] is the weight of the iᵗʰ stone. On each turn, smash the two heaviest stones together; if the weights are equal both are destroyed, otherwise a new stone of the weight difference remains. Return the weight of the last remaining stone, or 0 if none remain.

Examples

Input stones = [2, 7, 4, 1, 8, 1]; Output 1. Input stones = [1]; Output 1. Input stones = [1, 3]; Output 2

Constraints

1 ≤ stones.length ≤ 30 1 ≤ stones[i] ≤ 1000

Practice Last Stone Weight free on ExecCode. Browse DSA problems, topic map, and placement guides.