Check if Queue Elements are pairwise consecutive

Given a Queue of integers. The task is to check if consecutive elements in the queue are pairwise consecutive.

Examples:

Input : 1 2 5 6 9 10
Output : Yes

Input : 2 3 9 11 8 7
Output : No

Approach : Using two stacks :

  • Transfer all elements of the queue to one auxiliary stack aux.
  • Now, transfer the elements from this stack to another auxiliary stack aux2.
  • Start popping two elements from aux2 and then check the difference between these two elements. If their difference is 1, continue for other pairs till one element is left in the stack.
  • Also, after popping, push them to queue simultaneously to maintain the original queue.
  • Finally, if there is any pair with a difference not equal to 1, return false, else return true.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if successive
// pair of numbers in the queue are
// consecutive or not
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if elements are
// pairwise consecutive in queue
bool pairWiseConsecutive(queue<int> q)
{
    // Transfer elements of q to aux.
    stack<int> aux;
    while (!q.empty()) {
        aux.push(q.front());
        q.pop();
    }
  
    // Again transfer the
    // elements of aux to aux2
    stack<int> aux2;
    while (!aux.empty()) {
        aux2.push(aux.top());
        aux.pop();
    }
  
    // Traverse aux2 and see if
    // elements are pairwise
    // consecutive or not. We also
    // need to make sure that original
    // content is retained.
    bool result = true;
    while (aux2.size() > 1) {
  
        // Fetch current top two
        // elements of aux2 and check
        // if they are consecutive.
        int x = aux2.top();
        aux2.pop();
  
        int y = aux2.top();
        aux2.pop();
  
        if (abs(x - y) != 1)
            result = false;
  
        // Push the elements to queue
        q.push(x);
        q.push(y);
    }
  
    if (aux2.size() == 1)
        q.push(aux2.top());
  
    return result;
}
  
// Driver program
int main()
{
    // Pushing elements into the queue
    queue<int> q;
    q.push(4);
    q.push(5);
    q.push(-2);
    q.push(-3);
    q.push(11);
    q.push(10);
    q.push(5);
    q.push(6);
  
    if (pairWiseConsecutive(q))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
  
    // Printing the original queue
    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if successive 
// pair of numbers in the queue are 
// consecutive or not 
  
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
  
  
public class GFG {
  
// Function to check if elements are 
// pairwise consecutive in queue 
static boolean pairWiseConsecutive(Queue<Integer> q) 
    // Transfer elements of q to aux. 
    Stack<Integer> aux = new Stack<>(); 
    while (!q.isEmpty()) { 
        aux.push(q.peek()); 
        q.poll(); 
    
  
    // Again transfer the 
    // elements of aux to aux2 
    Stack<Integer> aux2 = new Stack<>();
    while (!aux.empty()) { 
        aux2.push(aux.peek()); 
        aux.pop(); 
    
  
    // Traverse aux2 and see if 
    // elements are pairwise 
    // consecutive or not. We also 
    // need to make sure that original 
    // content is retained. 
    boolean result = true
    while (aux2.size() > 1) { 
  
        // Fetch current top two 
        // elements of aux2 and check 
        // if they are consecutive. 
        int x = aux2.peek(); 
        aux2.pop(); 
  
        int y = aux2.peek(); 
        aux2.pop(); 
  
        if (Math.abs(x - y) != 1
            result = false
  
        // Push the elements to queue 
        q.add(x); 
        q.add(y); 
    
  
    if (aux2.size() == 1
        q.add(aux2.peek()); 
  
    return result; 
  
// Driver program 
  static public void main(String[] args) {
        // Pushing elements into the queue 
    Queue<Integer> q= new LinkedList<Integer>(); 
    q.add(4); 
    q.add(5); 
    q.add(-2); 
    q.add(-3); 
    q.add(11); 
    q.add(10); 
    q.add(5); 
    q.add(6); 
  
    if (pairWiseConsecutive(q)) 
        System.out.println("Yes"); 
    else
        System.out.println("No"); 
  
    // Printing the original queue 
    while (!q.isEmpty()) { 
        System.out.print(q.peek() + " "); 
        q.remove(); 
    
    System.out.println(); 
  
    }
}
// This code is contributed by Rajput-Ji

chevron_right



Output:

Yes
4 5 -2 -3 11 10 5 6 

Time Complexity: O(n), where n is size of queue.
Auxiliary Space: O(n), where n is the size of stack.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.