#K58597. Modify Array by Removing Frequent Elements
Modify Array by Removing Frequent Elements
Modify Array by Removing Frequent Elements
You are given an array of integers. Your task is to modify the array by performing the following two operations:
- Remove every element that occurs more than twice in the entire array. In other words, if an element appears strictly more than 2 times, all occurrences of that element are removed.
- Reverse the order of the remaining elements.
If after these operations the resulting array is empty, output [-1]
.
Note: An element is kept if it appears at most two times in the original array. Mathematically, for an element x, it is kept if and only if its frequency satisfies \( frequency(x) \le 2 \).
inputFormat
The input is given via standard input in the following format:
n a1 a2 a3 ... an
Here, n
is the number of elements in the array, and the second line contains n
space-separated integers.
outputFormat
Output the modified array as a single line of space-separated integers. If the modified array is empty, output -1
.
7
1 2 3 2 3 3 4
4 2 2 1