# 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++

 `// C++ program to check if successive ` `// pair of numbers in the queue are ` `// consecutive or not ` `#include ` `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; ` `} `

## Java

 `// 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 q)  ` `{  ` `    ``// Transfer elements of q to aux.  ` `    ``Stack aux = ``new` `Stack<>();  ` `    ``while` `(!q.isEmpty()) {  ` `        ``aux.push(q.peek());  ` `        ``q.poll();  ` `    ``}  ` ` `  `    ``// Again transfer the  ` `    ``// elements of aux to aux2  ` `    ``Stack 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 q= ``new` `LinkedList();  ` `    ``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 `

## C#

 `// C# program to check if successive  ` `// pair of numbers in the queue are  ` `// consecutive or not  ` `using` `System; ` `using` `System.Collections.Generic;  ` `     `  `class` `GFG  ` `{ ` ` `  `// Function to check if elements are  ` `// pairwise consecutive in queue  ` `static` `Boolean pairWiseConsecutive(Queue<``int``> q)  ` `{  ` `    ``// Transfer elements of q to aux.  ` `    ``Stack<``int``> aux = ``new` `Stack<``int``>();  ` `    ``while` `(q.Count != 0) ` `    ``{  ` `        ``aux.Push(q.Peek());  ` `        ``q.Dequeue();  ` `    ``}  ` ` `  `    ``// Again transfer the  ` `    ``// elements of aux to aux2  ` `    ``Stack<``int``> aux2 = ``new` `Stack<``int``>(); ` `    ``while` `(aux.Count != 0)  ` `    ``{  ` `        ``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.Count > 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.Enqueue(x);  ` `        ``q.Enqueue(y);  ` `    ``}  ` ` `  `    ``if` `(aux2.Count == 1)  ` `        ``q.Enqueue(aux2.Peek());  ` ` `  `    ``return` `result;  ` `}  ` ` `  `// Driver code  ` `static` `public` `void` `Main(String[] args)  ` `{ ` `    ``// Pushing elements into the queue  ` `    ``Queue<``int``> q = ``new` `Queue<``int``>();  ` `    ``q.Enqueue(4);  ` `    ``q.Enqueue(5);  ` `    ``q.Enqueue(-2);  ` `    ``q.Enqueue(-3);  ` `    ``q.Enqueue(11);  ` `    ``q.Enqueue(10);  ` `    ``q.Enqueue(5);  ` `    ``q.Enqueue(6);  ` ` `  `    ``if` `(pairWiseConsecutive(q))  ` `        ``Console.WriteLine(``"Yes"``);  ` `    ``else` `        ``Console.WriteLine(``"No"``);  ` ` `  `    ``// Printing the original queue  ` `    ``while` `(q.Count != 0)  ` `    ``{  ` `        ``Console.Write(q.Peek() + ``" "``);  ` `        ``q.Dequeue();  ` `    ``}  ` `    ``Console.WriteLine();  ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

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, 29AjayKumar

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.