The Wayback Machine - https://web.archive.org/web/20220420132708/https://www.geeksforgeeks.org/find-common-elements-of-stack-and-queue/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find common elements of Stack and Queue

  • Last Updated : 20 Apr, 2022

Given a stack of M elements and a queue of N elements in sorted order. The task is to find out the common elements of the stack and the queue.

Examples:

Input: stack = [1, 3, 5, 7], queue = [1, 2, 5, 9]
Output: 5, 1
Explanation: 1 and 5 is present in both stack and queue.

Input: stack = [1, 3], queue = [2, 4]
Output: Not Found
Explanation: There is no common element.

 

Approach: The given problem can be solved with the help of the following idea:

As both are sorted, the top element of the stack will be the maximum and the front of the queue will be the minimum. So reverse any of them and compare the elements in top of stack and front of queue to find the common elements.

Follow the illustration below for a better understanding.

Illustration:

Say, stack = [1, 3, 5, 7] where 7 is at the top and 
the queue = [1, 2, 5, 9] where 1 is at the front.

Say we are reversng the queue. Do the following to reverse the queue:

  • In first step:
    One by one pop the element from the queue(i.e., all the elements of queue) and push into stack.
            => After First Iteration, Stack = [1, 3, 5, 7, 1] and Queue = [2, 5, 9]
            => After Second Iteration, Stack = [1, 3, 5, 7, 1, 2] and Queue = [5, 9]
            => After Third Iteration, Stack = [1, 3, 5, 7, 1, 2, 5] and Queue = [9]
            => After Fourth Iteration, Stack = [1, 3, 5, 7, 1, 2, 5, 9] and Queue = []
  • In second step:
           => One by one pop the element from the stack(i.e., coming from queue)  and push into queue.
           => After First Iteration, Stack = [1, 3, 5, 7, 1, 2, 5] and Queue = [9]
           => After Second Iteration, Stack = [1, 3, 5, 7, 1, 2] and Queue = [9, 5]
           => After Third Iteration, Stack = [1, 3, 5, 7, 1] and Queue = [9, 5, 2]
           => After Fourth Iteration, Stack = [1, 3, 5, 7] and Queue = [9, 5, 2, 1]

Now the following for finding the common elements.

1st Step:
        => stack top < queue front.
        => Pop queue front.
        => So stack is [1, 3, 5, 7] and queue [5, 2, 1]

2nd step:
        => stack top > queue front
        => Pop stack top
        => So stack [1, 3, 5] and queue [5, 2, 1]

3rd step:
        => stack top = queue front
        => Pop stack top and queue front
        => So stack [1, 3] and queue [2, 1]
        => Common elements [5]

4th step:
        => stack top > queue front
        => Pop stack top
        => So stack [1] and queue [2, 1]

5th Step:
        => stack top < queue front.
        => Pop queue front.
        => So stack is [1] and queue [1]

6th step:
        => stack top = queue front
        => Pop stack top and queue front
        => So stack [] and queue []
        => Common elements [5, 1].

Follow the below steps to solve the problem:

  • Reverse the Queue.
  • Traverse stack and queue while stack and queue do not become empty.
    • If top of stack = front of queue that is a common element.
    • Else if, top of stack > front of queue, pop the top element of the stack.
    • Else, top of stack < front of queue, pop the front element of the stack.
  • Print the common elements.

Below is the implementation of the above approach:

C++




// C++ code to implement the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find common element
// of stack and queue
vector<int> findCommonElement(stack<int>& St,
                              queue<int>& Q)
{
    // Initialize size of queue Q to 0
    int Size = 0;
    vector<int> v;
  
    // Put every element of queue into stack
    // and calculate size of queue
    while (!Q.empty()) {
        St.push(Q.front());
        Q.pop();
        Size++;
    }
  
    // Put extra element of stack into queue
    // again extra element of stack is the
    // element coming from queue. Now, the
    // queue is reverse
    while (Size != 0) {
        Q.push(St.top());
        St.pop();
        Size--;
    }
  
    // Traverse while stack and queue is not
    // empty
    while (!St.empty() && !Q.empty()) {
        // Top element of stack
        int a = St.top();
  
        // Front element of queue
        int b = Q.front();
  
        // Push the common element
        // in vector if a = b
        if (a == b)
            v.push_back(a);
  
        // Else pop the larger value
        // from its container
        (a > b) ? St.pop() : Q.pop();
    }
    return v;
}
  
// Driver Code
int main()
{
    stack<int> St;
    queue<int> Q;
  
    // Fill element into stack
    St.push(1);
    St.push(3);
    St.push(5);
    St.push(7);
  
    // Fill element into queue
    Q.push(1);
    Q.push(2);
    Q.push(5);
    Q.push(9);
  
    // Find common element if exists
    vector<int> v = findCommonElement(St, Q);
  
    if (v.size() == 0)
        cout << "Not Found" << endl;
    for (auto i : v)
        cout << i << " ";
    return 0;
}
Output
5 1 

Time Complexity: O(M+N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!