609 views
1 votes
1 votes
Consider a stack is implemented using an array. What is worst case time complexity of push operation?

give explanation

1 Answer

0 votes
0 votes

O(1) in both worst and best case, ONLY if we have a pointer to the location where the next element is to be inserted in the STACK. Thereby, if we have to push a new element into the stack, we execute

a[pointer++] = elementValue.;

To pop and element, we execute,

poppedValue = a[--pointer];

--

Altough unlikely, if we have lost the pointer that was used to keep track of the top element of the stack, then obviously we have to search for the top most element and then push element into the stack. Time Complexity in that case is O(n).

--

Hope I am right. :P

Related questions

1 votes
1 votes
1 answer
2
Na462 asked Aug 22, 2018
2,886 views
Consider array A[1..100,1..100],in which elements are stored in Z representation. An example of 5x5 such array is shown below: Base address of array = 1000,size of each e...
13 votes
13 votes
8 answers
3
Na462 asked Aug 22, 2018
5,470 views
Consider a 2 dimensional array A[40...95,40...95] in lower triangular matrix representation. The size of each element of array is 1 Byte.If array is implemented in memory...
0 votes
0 votes
1 answer
4
iarnav asked Jun 12, 2018
839 views
Given an sorted array in descending order, what will be the time complexity to delete the minimum element from this array?