# Help me with this problem! C++

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.
Input:
A string of positive numbers is given on 32 bits of the keyboard, unknown for a long time, they being separated by space.

Output:
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.

For example:
|Test|Input|Result|
|Test #1|8 7 9 5 7|4|
|Test #2|2 7 4 7 6|-1|

can you help me quickly please?

1 Like

Use multiset and simply check last two elements

I donâ€™t know how, can you help me solve the code?

If you donâ€™t know know multiset then sort the array and check last elements. But when you are inserting the new element , do it using insertion sort

You can read about multiset from here Multiset in C++ Standard Template Library (STL) - GeeksforGeeks

Canâ€™t you fix it?I tried and I couldnâ€™t make it.

Can you send the question link?
Are you asking to send the code?

I would like the code for this problem, because I donâ€™t know how to solve it

Where have you seen this question?

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.

History repeats itself How to solve this problem in c ++? - #7 by galencolin

4 Likes

I have some errors, can you help me with them?
I posted the image below

didnâ€™t see that coming

dude i thought you posted this just for fun â€¦if you are trying to cheat in anyways i canâ€™t help you

Is he posting everyday for asking solution?

1 Like

Yes, help!

tbh this is kinda sad now

2 Likes