Posted on by Kalkicode
Code Stack

# Check if stack elements are pairwise consecutive

The problem is to check if the elements of a stack are pairwise consecutive. A stack is pairwise consecutive if each pair of consecutive elements in the stack forms a consecutive pair of numbers. For example, if the stack contains elements [2, 1, 4, 3, 6, 5], then each pair of consecutive elements [2, 1], [4, 3], and [6, 5] are consecutive pairs.

## Example

1. For the stack [2, 1, 6, 5], the elements are pairwise consecutive as [2, 1] and [6, 5] are consecutive pairs.
2. For the stack [4, 3, 1, 7, 6], the elements are not pairwise consecutive as [4, 3] and [7, 6] are not consecutive pairs.

## Idea to Solve the Problem

To check if the stack elements are pairwise consecutive, we will use an auxiliary stack to store the elements temporarily while checking their pairwise consecutiveness. We will traverse the original stack, and for each pair of consecutive elements, we will compare them to see if they form a consecutive pair. If any pair is not consecutive, we will conclude that the stack is not pairwise consecutive.

## Algorithm

1. Create a stack `temp` to hold the elements temporarily.
2. Create two `Node` pointers `first` and `second` to keep track of the first and second elements of each pair.
3. Initialize a flag variable `status` to true. This variable will indicate whether the stack is pairwise consecutive or not.
4. While the original stack `top` is not empty and the `status` is true:
• Pop an element from the original stack and push it into the `temp` stack.
• Assign the top element of the `temp` stack to the `first` pointer.
• If the original stack is not empty, pop another element from it and push it into the `temp` stack.
• Assign the top element of the `temp` stack to the `second` pointer.
• Compare the data values of `first` and `second` nodes. If they are not consecutive, set `status` to false.
• If the original stack is empty at this point, break the loop.
5. After traversing the entire stack, if `status` is true, print "Pairwise sorted"; otherwise, print "Not Pairwise sorted".
6. Remove the elements from the `temp` stack and insert them back into the original stack to restore the original stack.

## Pseudocode

``````FUNCTION is_consecutive():
CREATE an auxiliary stack temp
CREATE two Node pointers first and second
CREATE a flag variable status and set it to true
WHILE the stack top is not NULL AND status is true:
PUSH the top element of the original stack into the temp stack
ASSIGN the top element of the temp stack to first
IF the original stack is not empty:
PUSH another element from the original stack into the temp stack
ASSIGN the top element of the temp stack to second
IF first.data is not consecutive with second.data:
SET status to false
IF the original stack is empty:
BREAK
IF status is true:
PRINT "Pairwise sorted"
ELSE:
PRINT "Not Pairwise sorted"
WHILE the temp stack is not empty:
POP an element from the temp stack and PUSH it back into the original stack

FUNCTION main():
CREATE a new MyStack object obj1
PUSH elements 5, 6, 1, and 2 into obj1
CALL obj1.is_consecutive()
CALL obj1.remove_nodes()

CREATE a new MyStack object obj2
PUSH elements 6, 7, 1, 3, and 4 into obj2
CALL obj2.is_consecutive()
CALL obj2.remove_nodes()
``````

## Code Solution

``````//C Program
//Check if stack elements are pairwise consecutive
#include <stdio.h>
//For malloc function
#include <stdlib.h>

//Define Stack Structure
struct Stack
{
int data;
struct Stack*next;
};

void push(int data,struct Stack**top)
{
//Create a new stack element
struct Stack*new_node = (struct Stack*)malloc(sizeof(struct Stack));

if(new_node!=NULL)
{
new_node->data=data;

new_node->next=*top;

*top=new_node;
}
else
{
printf("\nMemory Overflow\n");
}
}
//remove a top stack elements and return the value of top element
int pop(struct Stack**top)
{

if(*top!=NULL)
{
struct Stack*auxiliary=*top;

int data = auxiliary->data;

*top=auxiliary->next;

//Remove node
free(auxiliary);

auxiliary=NULL;

return data;

}
return -1;
}

struct Stack* is_consecutive(struct Stack*top)
{
//This stack variables are used to store actual stack element
struct Stack*temp=NULL;

//This is two auxiliary variables which are used to get pair elements
struct Stack*first=NULL;
struct Stack*second=NULL;

//This is a flag variable which is indicate result
int status = 1;

while(top!=NULL && status==1)
{
push(pop(&top),&temp);

//Get first pair element
first = temp;

if(top != NULL)
{
//When in case have two pair element exists
push(pop(&top),&temp);

//Get second pair element
second = temp;

//Compare two stack element
if(first->data != second->data && (second->data+1 != first->data))
{
//When pairwise elements are not consecutive
status=0;
}
}
}
if(status==1)
{
printf("Pairwise sorted");
}
else
{
printf("Not Pairwise sorted");
}
//Remove element of auxiliary stack and insert to actual stack
while(temp!=NULL)
{
//insert element into actual stack
push(pop(&temp),&top);
}

printf("\n");
}
//Remove given stack elements
void remove_elements(struct Stack**top)
{
struct Stack*temp = NULL;
while(*top != NULL)
{
temp=*top;
*top = temp->next;
printf("%d ",temp->data);
free(temp);
temp=NULL;
}
printf("\n");

}
int main()
{
struct Stack *top1=NULL;

push(5, &top1);
push(6, &top1);
push(1, &top1);
push(2, &top1);

top1 = is_consecutive(top1);
//Remove remaining elements into stack
remove_elements(&top1);

struct Stack *top2 = NULL;
//Case 2
push(6, &top2);
push(7, &top2);
push(1, &top2);
push(3, &top2);
push(4, &top2);

top2 = is_consecutive(top2);

//Remove remaining elements into stack
remove_elements(&top2);

return 0;
}```
```

#### Output

``````Pairwise sorted
2 1 6 5
Not Pairwise sorted
4 3 1 7 6``````
``````/*
C++ Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
#include<iostream>

using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
class MyStack {
public:
Node *top;
MyStack() {
this->top = NULL;
}
//Add a new element in stack
void push(int data) {
//Make a new stack node
Node *new_node = new Node(data);
if (new_node != NULL) {
new_node->next = this->top;
this->top = new_node;
} else {
cout << "Memory overflow\n";
}
}
//Add a top element in stack
//When stack is empty it returns -1
int pop() {
int temp = -1;
if (this->top != NULL) {
temp = this->top->data;
this->top = this->top->next;
}
return temp;
}
void is_consecutive() {
//This stack variables are used to store actual stack element
MyStack temp =  MyStack();
//This is two auxiliary variables which are used to get pair elements
Node *first = NULL;
Node *second = NULL;
//This is a flag variable which is indicate result
bool status = true;
while (this->top != NULL &&
status == true) {
temp.push(this->pop());
//Get first pair element
first = temp.top;
if (this->top != NULL) {
temp.push(this->pop());
//Get second pair element
second = temp.top;
//Compare two stack element

if (first->data != second->data &&
(second->data + 1 != first->data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
break;
}
}
if (status == true) {
cout << "Pairwise sorted : ";
} else {
cout << "Not Pairwise sorted : ";
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != NULL) {
//insert element into actual stack
this->push(temp.pop());
}
}
void remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (this->top != NULL) {
cout << " " << this->pop();
}
cout << "\n";
}
};
int main() {
MyStack obj1 =  MyStack();
obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
MyStack obj2 =  MyStack();
//Case 2
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
return 0;
}```
```

#### Output

``````Pairwise sorted :  2 1 6 5
Not Pairwise sorted :  4 3 1 7 6``````
``````/*
Java Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node
{
public int data;
public Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
public class MyStack {

public Node top;

public MyStack()
{
top = null;
}
//Add a new element in stack
public void push(int data)
{
//Make a new stack node
Node new_node = new Node(data);
if(new_node!=null)
{
new_node.next=top;
top = new_node;
}
else
{
System.out.print("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1
public int pop()
{
int temp = -1;
if(top!=null)
{
temp = top.data;
top = top.next;
}
return temp;
}
public void is_consecutive()
{
//This stack variables are used to store actual stack element
MyStack temp = new MyStack();
//This is two auxiliary variables which are used to get pair elements
Node first = null;
Node second = null;

//This is a flag variable which is indicate result
boolean status = true;
while (this.top  != null && status == true)
{
temp.push(this.pop());

//Get first pair element
first = temp.top;

if (this.top != null)
{

temp.push(this.pop());
//Get second pair element
second = temp.top;
//Compare two stack element

if (first.data != second.data && (second.data + 1 != first.data))
{
//When pairwise elements are not consecutive
status = false;
}
}
else
{
break;
}
}
if (status == true)
{
System.out.print("Pairwise sorted : ");
}
else
{
System.out.print("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != null)
{
//insert element into actual stack
this.push(temp.pop());
}

}
public void remove_nodes()
{
//This function is printing stack element and remove Existing nodes
while(this.top!=null)
{
System.out.print(" "+this.pop());
}
System.out.print("\n");
}
public static void main(String[] args) {

MyStack obj1 = new MyStack();

obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);

obj1.is_consecutive();
obj1.remove_nodes();

MyStack obj2 = new MyStack();

//Case 2
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();

}
}```
```

#### Output

``````Pairwise sorted :   2  1  6  5
Not Pairwise sorted :   4  3  1  7  6``````
``````/*
C# Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
using System;
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class MyStack {
public Node top;
public MyStack() {
top = null;
}
//Add a new element in stack
public void push(int data) {
//Make a new stack node
Node new_node = new Node(data);
if (new_node != null) {
new_node.next = top;
top = new_node;
} else {
Console.Write("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1
public int pop() {
int temp = -1;
if (top != null) {
temp = top.data;
top = top.next;
}
return temp;
}
public void is_consecutive() {
//This stack variables are used to store actual stack element
MyStack temp = new MyStack();
//This is two auxiliary variables which are used to get pair elements
Node first = null;
Node second = null;
//This is a flag variable which is indicate result
Boolean status = true;
while (this.top != null &&
status == true) {
temp.push(this.pop());
//Get first pair element
first = temp.top;
if (this.top != null) {
temp.push(this.pop());
//Get second pair element
second = temp.top;
//Compare two stack element

if (first.data != second.data &&
(second.data + 1 != first.data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
break;;
}
}
if (status == true) {
Console.Write("Pairwise sorted : ");
} else {
Console.Write("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != null) {
this.push(temp.pop());
}
}
public void remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (this.top != null) {
Console.Write(" " + this.pop());
}
Console.Write("\n");
}
public static void Main(String[] args) {
MyStack obj1 = new MyStack();
obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
MyStack obj2 = new MyStack();
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
}
}```
```

#### Output

``````Pairwise sorted :  2 1 6 5
Not Pairwise sorted :  4 3 1 7 6``````
``````<?php
/*
Php Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node {
public \$data;
public \$next;

function __construct(\$data) {
\$this->data = \$data;
\$this->next = null;
}
}
class MyStack {
public \$top;

function __construct() {
\$this->top = null;
}
//Add a new element in stack

public 	function push(\$data) {
//Make a new stack node
\$new_node = new Node(\$data);
if (\$new_node != null) {
\$new_node->next = \$this->top;
\$this->top = \$new_node;
} else {
echo("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1

public 	function pop() {
\$temp = -1;
if (\$this->top != null) {
\$temp = \$this->top->data;
\$this->top = \$this->top->next;
}
return \$temp;
}
public 	function is_consecutive() {
//This stack variables are used to store actual stack element
\$temp = new MyStack();
//This is two auxiliary variables which are used to get pair elements
\$first = null;
\$second = null;
//This is a flag variable which is indicate result
\$status = true;
while (\$this->top != null &&
\$status == true) {
\$temp->push(\$this->pop());
//Get first pair element
\$first = \$temp->top;
if (\$this->top != null) {
\$temp->push(\$this->pop());
//Get second pair element
\$second = \$temp->top;
//Compare two stack element

if (\$first->data != \$second->data &&
(\$second->data + 1 != \$first->data)) {
//When pairwise elements are not consecutive
\$status = false;
}
} else {
break;
}
}
if (\$status == true) {
echo("Pairwise sorted : ");
} else {
echo("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while (\$temp->top != null) {
//insert element into actual stack
\$this->push(\$temp->pop());
}
}
public 	function remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (\$this->top != null) {
echo(" ". \$this->pop());
}
echo("\n");
}
}

function main() {
\$obj1 = new MyStack();
\$obj1->push(5);
\$obj1->push(6);
\$obj1->push(1);
\$obj1->push(2);
\$obj1->is_consecutive();
\$obj1->remove_nodes();
\$obj2 = new MyStack();
//Case 2
\$obj2->push(6);
\$obj2->push(7);
\$obj2->push(1);
\$obj2->push(3);
\$obj2->push(4);
\$obj2->is_consecutive();
\$obj2->remove_nodes();

}
main();```
```

#### Output

``````Pairwise sorted :  2 1 6 5
Not Pairwise sorted :  4 3 1 7 6``````
``````/*
Node Js Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class MyStack {
constructor() {
this.top = null;
}

//Add a new element in stack
push(data) {
//Make a new stack node
var new_node = new Node(data);
if (new_node != null) {
new_node.next = this.top;
this.top = new_node;
} else {
process.stdout.write("Memory overflow\n");
}
}

//Add a top element in stack
//When stack is empty it returns -1
pop() {
var temp = -1;
if (this.top != null) {
temp = this.top.data;
this.top = this.top.next;
}

return temp;
}
is_consecutive() {
//This stack variables are used to store actual stack element
var temp = new MyStack();
//This is two auxiliary variables which are used to get pair elements
var first = null;
var second = null;
//This is a flag variable which is indicate result
var status = true;
while (this.top != null &&
status == true) {
temp.push(this.pop());
//Get first pair element
first = temp.top;
if (this.top != null) {
temp.push(this.pop());
//Get second pair element
second = temp.top;
//Compare two stack element

if (first.data != second.data &&
(second.data + 1 != first.data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
break;
}
}

if (status == true) {
process.stdout.write("Pairwise sorted : ");
} else {
process.stdout.write("Not Pairwise sorted : ");
}

//Remove element of auxiliary stack and insert to actual stack
while (temp.top != null) {
//insert element into actual stack
this.push(temp.pop());
}
}
remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (this.top != null) {
process.stdout.write(" " + this.pop());
}

process.stdout.write("\n");
}
}

function main(args) {
var obj1 = new MyStack();
obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
var obj2 = new MyStack();
//Case 2
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
}

main();```
```

#### Output

``````Pairwise sorted :  2 1 6 5
Not Pairwise sorted :  4 3 1 7 6``````
``````#   Python 3 Program
#   Check if stack elements are pairwise consecutive

# Stack Node
class Node :

def __init__(self, data) :
self.data = data
self.next = None

class MyStack :

def __init__(self) :
self.top = None

# Add a new element in stack
def push(self, data) :
# Make a new stack node
new_node = Node(data)
if (new_node != None) :
new_node.next = self.top
self.top = new_node
else :
print("Memory overflow\n", end = "")

# Add a top element in stack
# When stack is empty it returns -1
def pop(self) :
temp = -1
if (self.top != None) :
temp = self.top.data
self.top = self.top.next

return temp

def is_consecutive(self) :
# This stack variables are used to store actual stack element
temp = MyStack()
# This is two auxiliary variables which are used to get pair elements
first = None
second = None
# This is a flag variable which is indicate result
status = True
while (self.top != None and status == True) :
temp.push(self.pop())
# Get first pair element
first = temp.top
if (self.top != None) :
temp.push(self.pop())
# Get second pair element
second = temp.top
# Compare two stack element

if (first.data != second.data and(second.data + 1 != first.data)) :
# When pairwise elements are not consecutive
status = False

else :
break

if (status == True) :
print("Pairwise sorted : ", end = "")
else :
print("Not Pairwise sorted : ", end = "")

# Remove element of auxiliary stack and insert to actual stack
while (temp.top != None) :
# insert element into actual stack
self.push(temp.pop())

def remove_nodes(self) :
# This function is printing stack element and remove Existing nodes
while (self.top != None) :
print(" ", self.pop(), end = "")

print("\n", end = "")

def main() :
obj1 = MyStack()
obj1.push(5)
obj1.push(6)
obj1.push(1)
obj1.push(2)
obj1.is_consecutive()
obj1.remove_nodes()
obj2 = MyStack()
# Case 2
obj2.push(6)
obj2.push(7)
obj2.push(1)
obj2.push(3)
obj2.push(4)
obj2.is_consecutive()
obj2.remove_nodes()

if __name__ == "__main__":
main()```
```

#### Output

``````Pairwise sorted :   2  1  6  5
Not Pairwise sorted :   4  3  1  7  6``````
``````#   Ruby Program
#   Check if stack elements are pairwise consecutive

# Stack Node
class Node
# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)
self.data = data
self.next = nil
end
end
class MyStack
# Define the accessor and reader of class MyStack
attr_accessor :top

def initialize()
@top = nil
end
# Add a new element in stack
def push(data)
# Make a new stack node
new_node = Node.new(data)
if (new_node != nil)
new_node.next = @top
@top = new_node
else
print("Memory overflow\n")
end
end
# Add a top element in stack
# When stack is empty it returns -1
def pop()
temp = -1
if (@top != nil)
temp = @top.data
@top = @top.next
end
return temp
end
def is_consecutive()
# This stack variables are used to store actual stack element
temp = MyStack.new()
# This is two auxiliary variables which are used to get pair elements
first = nil
second = nil
# This is a flag variable which is indicate result
status = true
while (self.top != nil &&
status == true)
temp.push(self.pop())
# Get first pair element
first = temp.top
if (self.top != nil)
temp.push(self.pop())
# Get second pair element
second = temp.top
# Compare two stack element

if (first.data != second.data &&
(second.data + 1 != first.data))
# When pairwise elements are not consecutive
status = false
end
else
break
end
end
if (status == true)
print("Pairwise sorted  :")
else
print("Not Pairwise sorted  :")
end
# Remove element of auxiliary stack and insert to actual stack
while (temp.top != nil)
# insert element into actual stack
self.push(temp.pop())
end
end
def remove_nodes()
# This function is printing stack element and remove Existing nodes
while (self.top != nil)
print(" ", self.pop())
end
print("\n")
end
end
def main()
obj1 = MyStack.new()
obj1.push(5)
obj1.push(6)
obj1.push(1)
obj1.push(2)
obj1.is_consecutive()
obj1.remove_nodes()
obj2 = MyStack.new()
# Case 2
obj2.push(6)
obj2.push(7)
obj2.push(1)
obj2.push(3)
obj2.push(4)
obj2.is_consecutive()
obj2.remove_nodes()
end
main()```
```

#### Output

``````Pairwise sorted  : 2 1 6 5
Not Pairwise sorted  : 4 3 1 7 6
``````
``````/*
Scala Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node(var next: Node,
var data: Int) {

def this(data: Int) {
this( null,data);
}
}
class MyStack(var top: Node) {

def this() {
this(null);
}
//Add a new element in stack
def push(data: Int): Unit = {
//Make a new stack node
var new_node: Node = new Node(data);

if (new_node != null) {
new_node.next = top;
top = new_node;
} else {
print("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1
def pop(): Int = {
var temp: Int = -1;

if (top != null) {
temp = top.data;
top = top.next;
}
return temp;
}
def is_consecutive(): Unit = {
//This stack variables are used to store actual stack element
var temp: MyStack = new MyStack();

//This is two auxiliary variables which are used to get pair elements
var first: Node = null;
var second: Node = null;

//This is a flag variable which is indicate result
var status: Boolean = true;
var in_out: Boolean = true;
while (this.top != null &&
status == true && in_out==true) {
temp.push(this.pop());

//Get first pair element
first = temp.top;

if (this.top != null) {
temp.push(this.pop());

//Get second pair element
second = temp.top;

//Compare two stack element

if (first.data != second.data &&
(second.data + 1 != first.data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
in_out=false;
}
}
if (status == true) {
print("Pairwise sorted : ");
} else {
print("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != null) {
//insert element into actual stack
this.push(temp.pop());
}
}
def remove_nodes(): Unit = {
//This function is printing stack element and remove Existing nodes
while (this.top != null) {
print(" " + this.pop());
}
print("\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj1: MyStack = new MyStack();

obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
var obj2: MyStack = new MyStack();

//Case 2
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
}
}```
```

#### Output

``````Pairwise sorted :  2 1 6 5
Not Pairwise sorted :  4 3 1 7 6``````
``````/*
Swift Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node {
var data: Int;
var next: Node? ;
init(_ data: Int) {
self.data = data;
self.next = nil;
}
}
class MyStack {
var top: Node? ;
init() {
self.top = nil;
}
//Add a new element in stack
func push(_ data: Int) {
//Make a new stack node
let new_node: Node? = Node(data);
if (new_node != nil) {
new_node!.next = self.top;
self.top = new_node;
} else {
print("Memory overflow\n", terminator: "");
}
}
//Add a top element in stack
//When stack is empty it returns -1
func pop() -> Int {
var temp: Int = -1;
if (self.top != nil) {
temp = self.top!.data;
self.top = self.top!.next;
}
return temp;
}
func is_consecutive() {
//This stack variables are used to store actual stack element
let temp: MyStack? = MyStack();
//This is two auxiliary variables which are used to get pair elements
var first: Node? = nil;
var second: Node? = nil;
//This is a flag variable which is indicate result
var status: Bool = true;
while (self.top != nil &&
status == true) {
temp!.push(self.pop());
//Get first pair element
first = temp!.top;
if (self.top != nil) {
temp!.push(self.pop());
//Get second pair element
second = temp!.top;
//Compare two stack element

if (first!.data != second!.data &&
(second!.data + 1 != first!.data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
break;
}
}
if (status == true) {
print("Pairwise sorted : ", terminator: "");
} else {
print("Not Pairwise sorted : ", terminator: "");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp!.top != nil) {
//insert element into actual stack
self.push(temp!.pop());
}
}
func remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (self.top != nil) {
print(" ", self.pop(), terminator: "");
}
print("\n", terminator: "");
}
}
func main() {
let obj1: MyStack? = MyStack();
obj1!.push(5);
obj1!.push(6);
obj1!.push(1);
obj1!.push(2);
obj1!.is_consecutive();
obj1!.remove_nodes();
let obj2: MyStack? = MyStack();
//Case 2
obj2!.push(6);
obj2!.push(7);
obj2!.push(1);
obj2!.push(3);
obj2!.push(4);
obj2!.is_consecutive();
obj2!.remove_nodes();
}
main();```
```

#### Output

``````Pairwise sorted :   2  1  6  5
Not Pairwise sorted :   4  3  1  7  6``````

## Time Complexity Analysis

1. The `push` and `pop` operations for the stack take O(1) time.
2. The `is_consecutive` function traverses the stack once, and each operation inside the loop takes O(1) time. Hence, the time complexity for the `is_consecutive` function is O(n), where n is the number of elements in the stack.

## Output Explanation

The output shows whether the elements of the stack are pairwise consecutive or not. For example, for the stack [2, 1, 6, 5], the output is "Pairwise sorted", indicating that the elements are pairwise consecutive. For the stack [4, 3, 1, 7, 6], the output is "Not Pairwise sorted", indicating that the elements are not pairwise consecutive. The code also prints the elements of the stack after the check is performed.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post