Recovering the original values from given information.

80 Views Asked by At

We have some N numbers[1..N] and N students. Originally we assign each number to single student.

Call this assignment as the initial state of the assignments.

Instance of assignment is described as this.

Suppose the student i(student having number i assigned) left, so all the students with assigned value greater then i will decrease their values by 1.

Hence, the assignment of N-1 numbers will be unique to all N-1 students.

We are given N instances of the assignment such that in each instance one student was left. But, No two instances has same student left. All the students left exactly one time.

Given instances are in not particular order.

How can we get any possible initial assignment, as there may more then one initial assignment is possible?

EDIT: sorry for adding this bit late.

We need to get initial assignment means the original order in which students where assigned values.

Example:

Suppose N=5.

assigned order is 5,2,4,1,3.

Next N=5 instances will be.

4,1,3,2. 1st left

4,3,2,1. 2nd left

4,2,3,1. 3rd left

4,2,1,3 4th left

2,4,1,3. 5th left

ith left meaning student with value i left.

I gave this instances in order, we do not know the order of instance like in given instance who left.

2

There are 2 best solutions below

3
On BEST ANSWER

So you could do this kinda brutishly. I'll walk through my approach using your particular example. We take the first "instance of the students in a line after one has left," in your case $\{4, 1, 3, 2\}$, and build every possible initial sequence it could have come from. Suppose number $5$ left. There are five different places he could have come from: $$ \{5, 4, 1, 3, 2\} \quad \{4, 5, 1, 3, 2\} \quad \{4, 1, 5, 3, 2\} \quad \{4, 1, 3, 5, 2\} \quad \{4, 1, 3, 2, 5\} $$ Next we suppose number $4$ left and consider the five different places he could have come from: $$ \{4, 5, 1, 3, 2\} \quad \{5, 4, 1, 3, 2\} \quad \{5, 1, 4, 3, 2\} \quad \{5, 1, 3, 4, 2\} \quad \{5, 1, 3, 2, 4\} $$

And so on, building all $25$ of these (removing duplicates if we feel like). The basic idea here is that if we were to build this full list of $25$ possible starting sequences for each "instance of the students in a line after one has left," then the true initial sequence must be common to all of these lists.

Next we would move onto the next "instance of the students in a line after one has left." But this time while we build our list of $25$ possible initial sequences, we only keep a result in the list if it also appears as one of the $25$ possible sequences we calculated from the prior "instance of the students in a line after one has left."

We repeat this until we have gone through each of the "instance of the students in a line after one has left" and any sequences that we have found common to each of the lists we built is a possible initial sequence. (I would imagine that there will be unique initial sequence, but I haven't thought too hard about it).

This solution is brutish since if we started with $N$ students there will be $N^2$ different sequences to build for each "instance of the students in a line after one has left", making the run-time complexity at least $\operatorname{O}(N^3)$. This solution can only be appropriately used if we know $N$ is sufficiently small.

1
On

Since I cannot comment I am posting this as an answer.

@mapierce271 the order is not O(N^3) as O(N) for comparing two lists which you are not considering even if hash map is used.

Mark this Correction.