Check if Queue Elements are pairwise consecutive
Last Updated :
01 Sep, 2022
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 the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool pairWiseConsecutive(queue< int > q)
{
stack< int > aux;
while (!q.empty()) {
aux.push(q.front());
q.pop();
}
stack< int > aux2;
while (!aux.empty()) {
aux2.push(aux.top());
aux.pop();
}
bool result = true ;
while (aux2.size() > 1) {
int x = aux2.top();
aux2.pop();
int y = aux2.top();
aux2.pop();
if ( abs (x - y) != 1)
result = false ;
q.push(x);
q.push(y);
}
if (aux2.size() == 1)
q.push(aux2.top());
return result;
}
int main()
{
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;
while (!q.empty()) {
cout << q.front() << " " ;
q.pop();
}
cout << endl;
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class GFG {
static boolean pairWiseConsecutive(Queue<Integer> q)
{
Stack<Integer> aux = new Stack<>();
while (!q.isEmpty()) {
aux.push(q.peek());
q.poll();
}
Stack<Integer> aux2 = new Stack<>();
while (!aux.empty()) {
aux2.push(aux.peek());
aux.pop();
}
boolean result = true ;
while (aux2.size() > 1 ) {
int x = aux2.peek();
aux2.pop();
int y = aux2.peek();
aux2.pop();
if (Math.abs(x - y) != 1 )
result = false ;
q.add(x);
q.add(y);
}
if (aux2.size() == 1 )
q.add(aux2.peek());
return result;
}
static public void main(String[] args) {
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" );
while (!q.isEmpty()) {
System.out.print(q.peek() + " " );
q.remove();
}
System.out.println();
}
}
|
Python3
import queue
def pairWiseConsecutive(q):
aux = []
while (q.qsize() ! = 0 ):
aux.append(q.queue[ 0 ])
q.get()
aux2 = []
while ( len (aux) ! = 0 ):
aux2.append(aux[ len (aux) - 1 ])
aux.pop()
result = bool ( True )
while ( len (aux2) > 1 ):
x = aux2[ len (aux2) - 1 ]
aux2.pop()
y = aux2[ len (aux2) - 1 ]
aux2.pop()
if ( abs (x - y) ! = 1 ):
result = bool ( False )
q.put(x)
q.put(y)
if ( len (aux2) = = 1 ):
q.put(aux2[ len (aux2) - 1 ])
return result
q = queue.Queue()
q.put( 4 )
q.put( 5 )
q.put( - 2 )
q.put( - 3 )
q.put( 11 )
q.put( 10 )
q.put( 5 )
q.put( 6 )
if (pairWiseConsecutive(q)):
print ( "Yes" )
else :
print ( "No" )
while ( not q.empty()):
print (q.queue[ 0 ] ,
end = " " )
q.get()
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Boolean pairWiseConsecutive(Queue< int > q)
{
Stack< int > aux = new Stack< int >();
while (q.Count != 0)
{
aux.Push(q.Peek());
q.Dequeue();
}
Stack< int > aux2 = new Stack< int >();
while (aux.Count != 0)
{
aux2.Push(aux.Peek());
aux.Pop();
}
Boolean result = true ;
while (aux2.Count > 1)
{
int x = aux2.Peek();
aux2.Pop();
int y = aux2.Peek();
aux2.Pop();
if (Math.Abs(x - y) != 1)
result = false ;
q.Enqueue(x);
q.Enqueue(y);
}
if (aux2.Count == 1)
q.Enqueue(aux2.Peek());
return result;
}
static public void Main(String[] args)
{
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" );
while (q.Count != 0)
{
Console.Write(q.Peek() + " " );
q.Dequeue();
}
Console.WriteLine();
}
}
|
Javascript
<script>
function pairWiseConsecutive(q)
{
var aux = [];
while (q.length!=0) {
aux.push(q[0]);
q.shift();
}
var aux2 = [];
while (aux.length!=0) {
aux2.push(aux[aux.length-1]);
aux.pop();
}
var result = true ;
while (aux2.length > 1) {
var x = aux2[aux2.length-1];
aux2.pop();
var y = aux2[aux2.length-1];
aux2.pop();
if (Math.abs(x - y) != 1)
result = false ;
q.push(x);
q.push(y);
}
if (aux2.length == 1)
q.push(aux2[aux2.length-1]);
return result;
}
var 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))
document.write( "Yes<br>" );
else
document.write( "No<br>" );
while (q.length!=0) {
document.write( q[0] + " " );
q.shift();
}
document.write( "<br>" );
</script>
|
Output
Yes
4 5 -2 -3 11 10 5 6
Complexity Analysis:
- Time Complexity: O(n), where n is the size of the queue.
- Auxiliary Space: O(n), where n is the size of the stack.
Share your thoughts in the comments
Please Login to comment...