Sudo Placement[1.3] | Playing with Stacks
Last Updated :
17 Jan, 2023
You are given 3 stacks, A(Input Stack), B(Auxiliary Stack) and C(Output Stack). Initially stack A contains numbers from 1 to N, you need to transfer all the numbers from stack A to stack C in sorted order i.e in the end, the stack C should have smallest element at the bottom and largest at top. You can use stack B i.e at any time you can push/pop elements to stack B also. At the end stack A, B should be empty.
Examples:
Input: A = {4, 3, 1, 2, 5}
Output: Yes 7
Input: A = {3, 4, 1, 2, 5}
Output: No
Approach: Iterate from the bottom of the given stack. Initialize required as the bottom most element in stackC at the end i.e., 1. Follow the given below algorithm to solve the above problem.
- if the stack element is equal to the required element, then the number of transfers will be one which is the count of transferring from A to C.
- if it is not equal to the required element, then check if it is possible to transfer it by comparing it with the topmost element in the stack.
- If the topmost element in stackC is greater than the stackA[i] element, then it is not possible to transfer it in a sorted way,
- else push the element to stackC and increment transfer.
- Iterate in the stackC and pop out the top most element until it is equal to the required and increment required and transfer in every steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countSteps( int sa[], int n)
{
stack< int > sc;
int required = 1, transfer = 0;
for ( int i = 0; i < n; i++) {
if (sa[i] == required) {
required++;
transfer++;
}
else {
if (!sc.empty() && sc.top() < sa[i]) {
cout << "NO" ;
return ;
}
else {
sc.push(sa[i]);
transfer++;
}
}
while (!sc.empty() && sc.top() == required) {
required++;
sc.pop();
transfer++;
}
}
cout << "YES " << transfer;
}
int main()
{
int sa[] = { 4, 3, 1, 2, 5 };
int n = sizeof (sa) / sizeof (sa[0]);
countSteps(sa, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void countSteps( int sa[], int n)
{
Stack<Integer> sc = new Stack<Integer>();
int required = 1 , transfer = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (sa[i] == required)
{
required++;
transfer++;
}
else
if (!sc.empty() && sc.peek() < sa[i])
{
System.out.print( "NO" );
return ;
}
else
{
sc.push(sa[i]);
transfer++;
}
while (!sc.empty() && sc.peek() == required)
{
required++;
sc.pop();
transfer++;
}
}
System.out.println( "YES " + transfer);
}
public static void main(String[] args)
{
int sa[] = { 4 , 3 , 1 , 2 , 5 };
int n = sa.length;
countSteps(sa, n);
}
}
|
Python3
from typing import List
def countSteps(sa: List [ int ], n: int ) - > None :
sc = []
required = 1
transfer = 0
for i in range (n):
if (sa[i] = = required):
required + = 1
transfer + = 1
else :
if (sc and sc[ - 1 ] < sa[i]):
print ( "NO" )
return
else :
sc.append(sa[i])
transfer + = 1
while (sc and sc[ - 1 ] = = required):
required + = 1
sc.pop()
transfer + = 1
print ( "YES {}" . format (transfer))
if __name__ = = "__main__" :
sa = [ 4 , 3 , 1 , 2 , 5 ]
n = len (sa)
countSteps(sa, n)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void countSteps( int []sa, int n)
{
Stack< int > sc = new Stack< int >();
int required = 1, transfer = 0;
for ( int i = 0; i < n; i++)
{
if (sa[i] == required)
{
required++;
transfer++;
}
else
if (sc.Count!=0 && sc.Peek() < sa[i])
{
Console.Write( "NO" );
return ;
}
else
{
sc.Push(sa[i]);
transfer++;
}
while (sc.Count!=0 && sc.Peek() == required)
{
required++;
sc.Pop();
transfer++;
}
}
Console.WriteLine( "YES " + transfer);
}
public static void Main(String[] args)
{
int []sa = {4, 3, 1, 2, 5};
int n = sa.Length;
countSteps(sa, n);
}
}
|
Javascript
<script>
function countSteps(sa, n)
{
let sc = [];
let required = 1, transfer = 0;
for (let i = 0; i < n; i++)
{
if (sa[i] == required)
{
required++;
transfer++;
}
else
if (sc.length!=0 && sc[sc.length-1] < sa[i])
{
document.write( "NO" );
return ;
}
else
{
sc.push(sa[i]);
transfer++;
}
while (sc.length!=0 && sc[sc.length-1] == required)
{
required++;
sc.pop();
transfer++;
}
}
document.write( "YES " + transfer+ "<br>" );
}
let sa=[4, 3, 1, 2, 5];
let n = sa.length;
countSteps(sa, n);
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...