396 views
0 votes
0 votes
How many number of add and remove operations are required to access 26th element of a queue of 50 elements,so that the original queue remains the same after the access is

Note:-Direct accessing through the index is not included.

2 Answers

0 votes
0 votes
Let’s do this with examples:

 

For, $Q.size=3$

$[1,2,3]$

$[3,1,2] (pop, push) \implies \text{Here 3 is accessed and re-pushed}$

$[2,3,1] (pop, push) \implies \text{Here 2 is accessed and re-pushed}$

$[1,2,3] (pop, push) \implies \text{Here 1 is accessed and re-pushed}$

Here, since $pop, push$ are both considered accesses,

Irrespective of which element is accessed, to revert back to the original $Q$,

$\text{Total Access} = 6$

 

For, $Q.size=4$

$[1,2,3,4]$

$[4,1,2,3] (pop, push) \implies \text{Here 4 is accessed and re-pushed}$

$[3,4,1,2] (pop, push) \implies \text{Here 3 is accessed and re-pushed}$

$[2,3,4,1] (pop, push) \implies \text{Here 2 is accessed and re-pushed}$

$[1,2,3,4] (pop, push) \implies \text{Here 1 is accessed and re-pushed}$

Irrespective of which element is accessed, to revert back to the original $Q$,

$\text{Total Access} = 8$

 

For, $Q.size=5$

$[1,2,3,4,5]$

$[5,1,2,3,4] (pop, push) \implies \text{Here 5 is accessed and re-pushed}$

$[4,5,1,2,3] (pop, push) \implies \text{Here 4 is accessed and re-pushed}$

$[3,4,5,1,2] (pop, push) \implies \text{Here 3 is accessed and re-pushed}$

$[2,3,4,5,1] (pop, push) \implies \text{Here 2 is accessed and re-pushed}$

$[1,2,3,4,5] (pop, push) \implies \text{Here 1 is accessed and re-pushed}$

Irrespective of which element is accessed, to revert back to the original $Q$,

$\text{Total Access} = 10$

 

$\therefore$ Generally,

Assuming that an element can be accessed only via $pop()$ operation -

Irrespective of which element is accessed, to revert back to the original $Q$,

$(2\times Q.size)$ accesses are needed.

 

So, for $Q.size=50$, irrespective of which element is accessed,

$\text{Total Access} = 2 \times Q.size = 2 \times 50 = 100$
0 votes
0 votes
To access the 26th element in a queue of 50 elements, 25 add operations and 24 remove operations would be required. This is because in a queue, elements are added to the back and removed from the front, so to access the 26th element, 25 elements would need to be added to the back of the queue and 24 elements would need to be removed from the front. This would leave the 26th element at the front of the queue, where it can be accessed, while also maintaining the original order of the queue.

Related questions

0 votes
0 votes
2 answers
1
Overflow04 asked Oct 2, 2022
333 views
#include <stdio.h int main(){ int a[] = {5,3,7,2,4}; int *p = &a[3]; p -= *p; printf("%d ",*p); return 0; } output is 3.Why 2 * sizeof(int) is doene.?
1 votes
1 votes
0 answers
2
srestha asked Mar 6, 2019
749 views
void find(int x){ static int i=10,y=0; y=y+i; for(i;i>0;i=i-10){ if(x!=0) find(x-1); else{ printf("%d",y); } } }What will be output printed for find(4)?
0 votes
0 votes
1 answer
3
0 votes
0 votes
1 answer
4
Gurdeep Saini asked Jan 18, 2019
485 views
in a sorted array of n distinct element finding i th largest element take o(1) . true / false