YVuranoglu
YVuranoglu

Reputation: 17

Sequence conversion

Could you please help me to understand this problem:

Convert the input sequence of N (1 ≤ N ≤ 20) input numbers so that the subsequences of the same numbers are replaced with the first numbers of the subsequences. Each input number is in the range [1, 2 000 000 000].

For example, the input sequence 1 2 2 3 1 1 1 4 4 is converted into 1 2 3 1 4.

Input: First, the number T of test cases is given. Each test case is specified using two lines. The first one contains the number N and the second one contains the numbers of the sequence.

Output: The converted sequence. The result for each test case should be printed in a separate line.

Upvotes: 0

Views: 95

Answers (1)

Eric J.
Eric J.

Reputation: 150148

For example, the input sequence 1 2 2 3 1 1 1 4 4 is converted into 1 2 3 1 4.

It looks like the idea is to remove duplicate numbers that occur adjacent to each other when creating the output.

You can do that by just keeping a state variable recording what the previous value was. When you get a new value, compare it to the state value. If it's the same, skip. If different, output it and update the state variable. Remember to initialize the state variable to a value not found in the input stream (e.g. -1 should work in this case).

Upvotes: 2

Related Questions