I am facing wrong answer even though a number of test cases work properly.
Please check the code.
Importance of time : wrong answer
@mananaroramail,
In block following 124, you need to stop once the queue matches the original order.
see, we have two cases associated with this question
- Order in which all the processes are called.
- Order in which all the processes should have been executed.
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 first itr:
both the index elements is same for first iteration i.e 5 match 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 second iteration
both the index elements is 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 remaining test case wil 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 total time will be 5;
for the sixth iteration
4 match with 4
then total time will be 6
for the seventh iteration
3 match with 3
then total time will be 7
Thanks a lot.
It worked perfectly.