Importance of time

How to approach this problem ?

hello @ashwani225

  • The Idea is to Store the Calling order in Queue and the ideal order in an Array.
  • Then compare front element of the queue with the ideal order.
  • If it matches then increment the time and remove the element.
  • Else, Dequeue the element and enqueue it again.
  • The loop will run till the queue is consumed totally

code for reference

Please explain using a test case. Test case given in problem i can’t understand it

5 4 2 3 1
5 2 1 4 3
5 match with 5 , time -> 1

4 2 3 1
2 1 4 3
4 not match with 2, time -> 2 , push 4 at the end of queue

2 3 1 4
2 1 4 3
2 match with 2 , time -> 3

3 1 4
1 4 3
3 not match with 1, time -> 4 , push 3 at the end of queue

1 4 3
1 4 3
1 match with 1 , time -> 5

4 3
4 3
4 match with 4, time ->6

3
3
3 match with 3 , time -> 7

total it will take total 7 unit of time

please mark ur doubt as resolved