Quote:
if we have the same numbers both in the first and in the last 15 these numbers will be replaced.i just thought about that and it felt weird to me.there must be a mistake in this question
Not really: there are three numbers which can appear on both sides: 1, 4, and 9 because they are squares between 1 and 10
So the simplest efficient way to do this is to look for those three values on the right, and if you find one, look for a matching 1, 2, or 3 on the left.
If you find one, remove them.
If the indexes of the match are the same, stop processing.