Let be a string of positive numbers of unknown length. The following 2 operations will be performed until we have 0 or 1 element in a row:
If the 2 largest elements in the string are equal, they will be removed.
If the 2 largest elements in the string are different, they will be removed and the difference between the 2 elements (the largest minus the smallest) will be added to the string.
A string of positive numbers is given on 32 bits of the keyboard, unknown for a long time, they being separated by space.
If an item is left in a row, that item will be displayed. If no item is left in the row, -1 will be displayed.
just use a priority queue to store your data and keep on doing operation on them until it is empty of it size is one. it’s same as mentioned by @sebastian u can (multiset) too both perform the same function as per your requirement.