Last remaining value

Write codeEasy12 min

Your task

You are given a list of positive integers. Repeatedly perform this operation: take the two largest values x and y (with x >= y) out of the list; if x equals y, both disappear; otherwise put x - y back. Stop when at most one value remains. Return the final remaining value, or 0 if nothing remains. The starting list may also be empty (return 0) or hold a single value (return it). Example: values = [2, 7, 4, 1, 8, 1] gives 1.

Examples

final_value(values: list[int]) -> int
Example 1
Inputfinal_value([2, 7, 4, 1, 8, 1])
Output1