A queue is implemented using an array such that n ENQUEUE and DEQUEUE operations are performed efficiently. Which one of the following statements is CORRECT (n refers to the number of items in the queue)?
A
Worst case time complexity for both operations will be Ω(logn)
No worries! We‘ve got your back. Try BYJU‘S free classes today!
B
Both operations can be performed in O(1) time
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
C
At most one operation can be performed in O(1) time but the worst case time for the other operation will be Ω(n)
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
The worst case time complexity for both operations will be Ω(n)
No worries! We‘ve got your back. Try BYJU‘S free classes today!
Open in App
Solution
The correct option is B Both operations can be performed in O(1) time Implementing queue using array:
ENQUEUE Operation:
Check array full or not
if array is full
stop
else enter the element in the end of array;
which will take O(1) time.
DEQUEUE Operation:
Check array empty or not
if array is empty
stop
else delete the element from front of array and increment the head value (pointer to the starting element of array).
which will take O(1) time.
So for array implementation of queue, ENQUEUE
and DEQUEUE operation takes O(1) time.