Check if Queue Elements are pairwise consecutive | Set-2
Last Updated :
27 Oct, 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 :
- Take a variable n to store size of queue.
- Push an element to the queue which acts as marker.
- Now, If size of queue is odd, start popping a pair while n > 2. Also, while popping these pairs, check for their difference and decrease n by 2, if there difference is not equal to 1, set flag to false.
- If size of queue is even, start popping a pair while n > 1. Also, while popping these pairs, check for their difference and decrease n by 2, if their difference is not equal to 1, set flag to false.
- Finally, check flag, if it is true, it means elements in the queue are pairwise sorted, else not.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool pairWiseConsecutive(queue< int > q)
{
int n = q.size();
q.push(INT_MIN);
bool result = true ;
if (n % 2 != 0) {
while (n > 2) {
int x = q.front();
q.pop();
q.push(x);
n--;
int y = q.front();
q.pop();
q.push(y);
n--;
if ( abs (x - y) != 1) {
result = false ;
}
}
q.push(q.front());
q.pop();
q.pop();
}
else {
while (n > 1) {
int x = q.front();
q.pop();
q.push(x);
n--;
int y = q.front();
q.pop();
q.push(y);
n--;
if ( abs (x - y) != 1) {
result = false ;
}
}
q.pop();
}
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);
q.push(8);
if (pairWiseConsecutive(q))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
public class GFG {
static boolean pairWiseConsecutive(Queue<Integer> q) {
int n = q.size();
q.add(Integer.MAX_VALUE);
boolean result = true ;
if (n % 2 != 0 ) {
while (n > 2 ) {
int x = q.peek();
q.remove();
q.add(x);
n--;
int y = q.peek();
q.remove();
q.add(y);
n--;
if (Math.abs(x - y) != 1 ) {
result = false ;
}
}
q.add(q.peek());
q.remove();
q.remove();
} else {
while (n > 1 ) {
int x = q.peek();
q.remove();
q.add(x);
n--;
int y = q.peek();
q.remove();
q.add(y);
n--;
if (Math.abs(x - y) != 1 ) {
result = false ;
}
}
q.remove();
}
return result;
}
static public void main(String[] args) {
Queue<Integer> 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 );
q.add( 8 );
if (pairWiseConsecutive(q)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
import sys, queue
def pairWiseConsecutive(q):
n = q.qsize()
q.put( - sys.maxsize);
result = bool ( True )
if (n % 2 ! = 0 ):
while (n > 2 ):
x = q.queue[ 0 ]
q.get()
q.put(x)
n - = 1
y = q.queue[ 0 ]
q.get()
q.put(y)
n - = 1
if ( abs (x - y) ! = 1 ):
result = bool ( False )
q.put(q.queue[ 0 ])
q.get()
q.get()
else :
while (n > 1 ):
x = q.queue[ 0 ]
q.get()
q.put(x)
n - = 1
y = q.queue[ 0 ]
q.get()
q.put(y)
n - = 1
if ( abs (x - y) ! = 1 ):
result = bool ( False )
q.get()
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 )
q.put( 8 )
if ( bool (pairWiseConsecutive(q))):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Boolean pairWiseConsecutive(Queue< int > q)
{
int n = q.Count;
q.Enqueue( int .MaxValue);
Boolean result = true ;
if (n % 2 != 0)
{
while (n > 2)
{
int x = q.Peek();
q.Dequeue();
q.Enqueue(x);
n--;
int y = q.Peek();
q.Dequeue();
q.Enqueue(y);
n--;
if (Math.Abs(x - y) != 1)
{
result = false ;
}
}
q.Enqueue(q.Peek());
q.Dequeue();
q.Dequeue();
} else
{
while (n > 1)
{
int x = q.Peek();
q.Dequeue();
q.Enqueue(x);
n--;
int y = q.Peek();
q.Dequeue();
q.Enqueue(y);
n--;
if (Math.Abs(x - y) != 1)
{
result = false ;
}
}
q.Dequeue();
}
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);
q.Enqueue(8);
if (pairWiseConsecutive(q))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function pairWiseConsecutive(q) {
let n = q.length;
q.push(Number.MIN_VALUE);
let result = true ;
if (n % 2 != 0) {
while (n > 2) {
let x = q[0];
q.shift();
q.push(x);
n--;
let y = q[0];
q.shift();
q.push(y);
n--;
if (Math.abs(x - y) != 1) {
result = false ;
}
}
q.push(q[0]);
q.shift();
q.shift();
}
else {
while (n > 1) {
let x = q[0];
q.shift();
q.push(x);
n--;
let y = q[0];
q.shift();
q.push(y);
n--;
if (Math.abs(x - y) != 1) {
result = false ;
}
}
q.shift();
}
return result;
}
let 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);
q.push(8);
if (pairWiseConsecutive(q))
console.log( "Yes" );
else
console.log( "No" );
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...