#K35917. Sequence Compression
Sequence Compression
Sequence Compression
You are given a sequence of integers and a value max_value. Your task is to compress the sequence by retaining only the first occurrence of each number and filtering out any integer that is greater than max_value. Specifically, for each number in the sequence, if it has not appeared before and it is less than or equal to max_value, you should include it in the output.
Note: The order of the retained numbers must be the same as their first appearance in the sequence.
For example, given a sequence 3 3 2 2 5
with max_value equal to 4
, the compressed sequence is 3 2
because:
- The first
3
is kept, the second3
is ignored. - The first
2
is kept, the second2
is ignored. 5
is greater than4
so it is discarded.
Solve the problem by reading from stdin and writing to stdout. If no number in the input meets the criteria, print an empty line.
inputFormat
The input consists of three lines:
- An integer n, representing the length of the sequence.
- n space-separated integers representing the sequence.
- An integer max_value representing the maximum allowed value.
outputFormat
Print the compressed sequence as a single line of space-separated integers. The sequence should contain only the first occurrence of each element that is less than or equal to max_value. If no element qualifies, output an empty line.
## sample5
3 3 2 2 5
4
3 2