2,356 views
1 votes
1 votes
How many number of add and remove operations are required to access x/2th element of a queue of ‘x’ elements, so that the original queue remains the same after the access is
1) 8*x
2) 4*x-1
3) 4*x
4) 8*x-1

1 Answer

1 votes
1 votes
Assume that i have 5 elements 1 2 3 4 5
Queue 1 hold this 1 2 3 4 5
Queue 2 is empty
if x = 5 then x/2 = 3

Now Initially queue1 : 1 2 3 4 5 queue 2 ---
Remove 1 from Q1          Q1 : 2 3 4 5
Insert 1 into Q2            Q2 : 1

Remove 2 from Q1          Q1 :  3 4 5
Insetr1 2nto Q2            Q2 : 1 2

Remove 3 from Q1          Q1 :   4 5 // displayed but this is not to be counted

Remove 1 from Q2       Q2 : 2
 Insert 1 into Q1       Q1 : 4 5 1

Remove 2 from Q2       Q2 :
 Insert 2 into Q1       Q1 : 4 5 1 2

Remove 4 from Q1          Q1 :  5 1 2  
Insert 4  into Q2            Q2 :  4

Remove  5 from Q1          Q1 :   1 2  
Insert 5  into Q2            Q2 :  4 5

Remove 4 from Q2            Q2 : 5
Insert 4 to Q1               Q1 : 1 2 4

Remove 5 from Q2            Q2 :
Insert 5 to Q1               Q1 : 1 2 4 5

Counting number of insertion and deletion = 16 = 4*(x-1) = 4*4 = 16

Related questions

0 votes
0 votes
2 answers
1
Arpit Tripathi asked May 4, 2016
498 views
Which of the following groups of letters is the odd one out?1) PRTXZ2) FMKRD3) JMPTY4) BFJLS
0 votes
0 votes
2 answers
2