![]() |
VOOZH | about |
Given an empty queue and an array of elements, we need to perform the following two operations.
Filling a queue refers to the process of adding array elements to it. In a queue, elements are added to the rear or back of the queue.
Emptying a queue means removing elements from it. In a queue, elements are removed from the front of the queue, adhering to the FIFO rule.
Examples:
Input: arr[] = [1, 2, 3, 4, 5]
Output: 1 2 3 4 5
Explanation: Enqueue the elements of the array into a queue in the same order, then dequeue and print the elements, resulting in the same order as the input:1 2 3 4 5.Input: arr[] = [1, 6, 43, 1, 2, 0, 5]
Output: 1 6 43 1 2 0 5
Explanation: Enqueue the elements of the array into a queue in the same order, then dequeue and print the elements, resulting in the same order as the input: 1 6 43 1 2 0 5.
The approach uses a queue, which follows the First-In-First-Out (FIFO) principle. Elements are added to the back and removed from the front in the same order they were added. The first element inserted is the first one to be removed, ensuring they are processed in the order they arrive.
1 2 3 4 5
Time Complexity: O(n)
Auxiliary Space: O(n)