Check if the elements of stack are pairwise sorted
Last Updated :
05 Sep, 2022
Given a stack of integers, write a function pairWiseSorted() that checks whether numbers in the stack are pairwise sorted or not.
The pairs must be increasing, and if the stack has an odd number of elements, the element at the top is left out of a pair. The function should retain the original stack content.
Only following standard operations are allowed on the stack.
- push(X): Enter a element X on top of stack.
- pop(): Removes top element of the stack.
- empty(): To check if stack is empty.
Examples:
Input: 4, 5, 6, 7, 8, 9
Output: Yes
Input: 4, 9, 2, 1, 10, 8
Output: No
Approach: The idea is to use another stack.
- Create an auxiliary stack aux.
- Transfer contents of given stack to aux.
- Traverse aux. While traversing fetch top two elements and check if they are sorted or not.
- After checking put these elements back to original stack.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool pairWiseConsecutive(stack< int > s)
{
stack< int > aux;
while (!s.empty()) {
aux.push(s.top());
s.pop();
}
bool result = true ;
while (!aux.empty()) {
int x = aux.top();
aux.pop();
int y = aux.top();
aux.pop();
if (x > y)
result = false ;
s.push(x);
s.push(y);
}
if (aux.size() == 1)
s.push(aux.top());
return result;
}
int main()
{
stack< int > s;
s.push(4);
s.push(5);
s.push(-3);
s.push(-2);
s.push(10);
s.push(11);
s.push(5);
s.push(6);
if (pairWiseConsecutive(s))
cout << "Yes" << endl;
else
cout << "No" << endl;
cout << "Stack content (from top)"
" after function call\n" ;
while (s.empty() == false ) {
cout << s.top() << " " ;
s.pop();
}
return 0;
}
|
Java
import java.util.Stack;
class GFG {
static boolean pairWiseConsecutive(Stack<Integer> s) {
Stack<Integer> aux = new Stack<>();
while (!s.empty()) {
aux.push(s.peek());
s.pop();
}
boolean result = true ;
while (!aux.empty()) {
int x = aux.peek();
aux.pop();
int y = aux.peek();
aux.pop();
if (x > y) {
result = false ;
}
s.push(x);
s.push(y);
}
if (aux.size() == 1 ) {
s.push(aux.peek());
}
return result;
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push( 4 );
s.push( 5 );
s.push(- 3 );
s.push(- 2 );
s.push( 10 );
s.push( 11 );
s.push( 5 );
s.push( 6 );
if (pairWiseConsecutive(s)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
System.out.println( "Stack content (from top)"
+ " after function call" );
while (s.empty() == false ) {
System.out.print(s.peek() + " " );
s.pop();
}
}
}
|
Python 3
from collections import deque
def pairWiseConsecutive(s):
aux = deque()
while len (s) > 0 :
aux.append(s.pop())
result = True
while len (aux) ! = 0 :
x = aux.pop()
y = aux.pop()
if x > y:
result = False
s.append(x)
s.append(y)
if len (aux) = = 1 :
s.append(aux.pop())
return result
if __name__ = = "__main__" :
s = deque()
s.append( 4 )
s.append( 5 )
s.append( - 3 )
s.append( - 2 )
s.append( 10 )
s.append( 11 )
s.append( 5 )
s.append( 6 )
if pairWiseConsecutive(s):
print ( "Yes" )
else :
print ( "No" )
print ( "Stack content (from top) after function call" )
while len (s) > 0 :
print (s.pop(), end = " " )
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static bool pairWiseConsecutive(Stack< int > s) {
Stack< int > aux = new Stack< int >();
while (!(s.Count==0)) {
aux.Push(s.Peek());
s.Pop();
}
bool result = true ;
while (!(aux.Count==0)) {
int x = aux.Peek();
aux.Pop();
int y = aux.Peek();
aux.Pop();
if (x > y) {
result = false ;
}
s.Push(x);
s.Push(y);
}
if (aux.Count == 1) {
s.Push(aux.Peek());
}
return result;
}
public static void Main() {
Stack< int > s = new Stack< int >();
s.Push(4);
s.Push(5);
s.Push(-3);
s.Push(-2);
s.Push(10);
s.Push(11);
s.Push(5);
s.Push(6);
if (pairWiseConsecutive(s)) {
Console.WriteLine( "Yes" );
} else {
Console.WriteLine( "No" );
}
Console.WriteLine( "Stack content (from top)"
+ " after function call" );
while (!(s.Count == 0)) {
Console.Write(s.Peek() + " " );
s.Pop();
}
}
}
|
Javascript
<script>
function pairWiseConsecutive(s)
{
var aux = [];
while (s.length!=0) {
aux.push(s[s.length-1]);
s.pop();
}
var result = true ;
while (aux.length!=0) {
var x = aux[aux.length-1];
aux.pop();
var y = aux[aux.length-1];
aux.pop();
if (x > y)
result = false ;
s.push(x);
s.push(y);
}
if (aux.length == 1)
s.push(aux[aux.length-1]);
return result;
}
var s = [];
s.push(4);
s.push(5);
s.push(-3);
s.push(-2);
s.push(10);
s.push(11);
s.push(5);
s.push(6);
if (pairWiseConsecutive(s))
document.write( "Yes" + "<br>" );
else
document.write( "No" + "<br>" );
document.write( "Stack content (from top)" +
" after function call<br>" );
while (s.length!=0) {
document.write( s[s.length-1] + " " );
s.pop();
}
</script>
|
Output
Yes
Stack content (from top) after function call
6 5 11 10 -2 -3 5 4
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...