So I got a problem in a placement coding test, and I couldn’t solve it! I couldn’t find that particular problem on the internet, but I have an idea as to how to approach it, but I can’t code it! Here is the problem.
There is a list of unique n natural numbers in a particular order.
For every number Ni a card is made with two numbers written on it, one is the number before Ni i.e. Ni-1 and another is the number after Ni i.e. Ni+1.
For N1 and Nn, the corresponding cards are written with zeros, i.e. (0, N2) and (Nn-1,0).
You will have to print the numbers in the list in the correct order.
INPUT
The first line is a single integer n, denoting the number of numbers in the list.
Next n lines contain a pair of integers denoting the numbers written on the n cards.
OUTPUT
A single line containing n space-separated numbers.
Eg.
INPUT
3
0 102
102 0
101 103
OUTPUT
101 102 103
INPUT
4
0 2
2 4
1 3
3 0
OUTPUT
1 2 3 4
One feature I noticed is, the first and last numbers in the list are stated only once each, while all the other ones are stated twice every time in the input. It is like a graph problem, in this case, the graph is a single line with no branches!
Thanks in advance.