Importance of time question

sir pl explain how output is coming

@ayush15goel hey ayush
In this question, you have to find out the unit of time required to complete all the process such that a process is executed from the ideal order only when it exists at the same index in the calling order
there are two cases associated with this question
1)The calling order in which all the processes are called.
2)The ideal order in which all the processes should have been executed.

so how we will calculate the total time to complete all the process
given a sample test case
5
5 4 2 3 1
5 2 1 4 3
Executing a process takes 1 unit of time. Changing the position takes 1 unit of time.
for the first iteration
both the index elements are the same for the first iteration i.e 5 matches with 5
unit time is 1
so manage a variable as total_time and add the unit time into the total_time variable
now for the second iteration
both the index elements are not same i.e 4 doesn’t match with 2
so we will remove 4 from that index and add to the last so the changing the position will take 1 unit of time
so total time will be total_time=total_time+1;
i.e total time will be 2
now the test case will be remaining
2 3 1 4
2 1 4 3
now in third iteration 2 match with 2 i.e total_time =total_time+1;
so total time will be 3
and the remaining test case will be
3 1 4
1 4 3
now in forth iteration 3 doesn’t match with 1 so we remove 3 from that index and add to the last and
again Changing the position takes 1 unit of time.
so total time here will be total_time=total_time+1;
i.e total time will be 4
and it will arrange like
1 4 3
1 4 3
now for the fifth iteration
1 match with 1
then the total time will be 5;
for the sixth iteration
4 match with 4
then the total time will be 6
for the seventh iteration
3 match with 3
then the total time will be 7

1 Like