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

Can you send question link?

what link? I don’t understand what the link is.

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 :slight_smile: 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? :thinking:

1 Like

Yes, help!

tbh this is kinda sad now

2 Likes