Sort the given stack elements based on their modulo with K
Last Updated :
29 Jun, 2022
Given a stack of integers and an integer K, the task is to sort the elements of the given stack using another stack in the increasing order of their modulo with K. If two numbers have the same remainder then the smaller number should come first.
Examples
Input: stack = {10, 3, 2, 6, 12}, K = 4
Output: 12 2 6 10 3
{12, 2, 6, 10, 3} is the required sorted order as the modulo
of these elements with K = 4 is {2, 3, 2, 2, 0}
Input: stack = {3, 4, 5, 10, 11, 1}, K = 3
Output: 3 1 4 10 5 11
Approach: An approach to sort the elements of the stack using another temporary stack has been discussed in this article, the same approach can be used here to sort the elements based on their modulo with K, the only difference is that when the elements being compared give the same modulo value then they will be compared based on their values.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortStack(stack< int >& input, int k)
{
stack< int > tmpStack;
while (!input.empty()) {
int tmp = input.top();
input.pop();
while (!tmpStack.empty()) {
int tmpStackMod = tmpStack.top() % k;
int tmpMod = tmp % k;
if ((tmpStackMod > tmpMod)
|| (tmpStackMod == tmpMod
&& tmpStack.top() > tmp)) {
input.push(tmpStack.top());
tmpStack.pop();
}
else
break ;
}
tmpStack.push(tmp);
}
while (!tmpStack.empty()) {
input.push(tmpStack.top());
tmpStack.pop();
}
while (!input.empty()) {
cout << input.top() << " " ;
input.pop();
}
}
int main()
{
stack< int > input;
input.push(10);
input.push(3);
input.push(2);
input.push(6);
input.push(12);
int k = 4;
sortStack(input, k);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void sortStack(Stack<Integer> input, int k)
{
Stack<Integer> tmpStack = new Stack<Integer>();
while (!input.isEmpty())
{
int tmp = input.peek();
input.pop();
while (!tmpStack.isEmpty())
{
int tmpStackMod = tmpStack.peek() % k;
int tmpMod = tmp % k;
if ((tmpStackMod > tmpMod) ||
(tmpStackMod == tmpMod &&
tmpStack.peek() > tmp))
{
input.push(tmpStack.peek());
tmpStack.pop();
}
else
break ;
}
tmpStack.push(tmp);
}
while (!tmpStack.isEmpty())
{
input.push(tmpStack.peek());
tmpStack.pop();
}
while (!input.empty())
{
System.out.print(input.peek() + " " );
input.pop();
}
}
public static void main(String args[])
{
Stack<Integer> input = new Stack<Integer>();
input.push( 10 );
input.push( 3 );
input.push( 2 );
input.push( 6 );
input.push( 12 );
int k = 4 ;
sortStack(input, k);
}
}
|
Python3
def sortStack(input1, k):
tmpStack = []
while ( len (input1) ! = 0 ):
tmp = input1[ - 1 ]
input1.pop()
while ( len (tmpStack) ! = 0 ):
tmpStackMod = tmpStack[ - 1 ] % k
tmpMod = tmp % k
if ((tmpStackMod > tmpMod) or
(tmpStackMod = = tmpMod and
tmpStack[ - 1 ] > tmp)):
input1.append(tmpStack[ - 1 ])
tmpStack.pop()
else :
break
tmpStack.append(tmp)
while ( len (tmpStack) ! = 0 ):
input1.append(tmpStack[ - 1 ])
tmpStack.pop()
while ( len (input1) ! = 0 ):
print (input1[ - 1 ], end = " " )
input1.pop()
if __name__ = = "__main__" :
input1 = []
input1.append( 10 )
input1.append( 3 )
input1.append( 2 )
input1.append( 6 )
input1.append( 12 )
k = 4
sortStack(input1, k)
|
C#
using System;
using System.Collections;
class GFG{
static void sortStack(Stack input,
int k)
{
Stack tmpStack = new Stack();
while (input.Count != 0)
{
int tmp = ( int )input.Peek();
input.Pop();
while (tmpStack.Count != 0)
{
int tmpStackMod = ( int )tmpStack.Peek() % k;
int tmpMod = tmp % k;
if ((tmpStackMod > tmpMod) ||
(tmpStackMod == tmpMod &&
( int )tmpStack.Peek() > tmp))
{
input.Push(( int )tmpStack.Peek());
tmpStack.Pop();
}
else
break ;
}
tmpStack.Push(tmp);
}
while (tmpStack.Count != 0)
{
input.Push(( int )tmpStack.Peek());
tmpStack.Pop();
}
while (input.Count != 0)
{
Console.Write(( int )input.Peek() + " " );
input.Pop();
}
}
public static void Main( string [] args)
{
Stack input = new Stack();
input.Push(10);
input.Push(3);
input.Push(2);
input.Push(6);
input.Push(12);
int k = 4;
sortStack(input, k);
}
}
|
Javascript
<script>
function sortStack(input,k)
{
let tmpStack = [];
while (input.length!=0)
{
let tmp = input.pop();
while (tmpStack.length!=0)
{
let tmpStackMod = tmpStack[tmpStack.length-1] % k;
let tmpMod = tmp % k;
if ((tmpStackMod > tmpMod) ||
(tmpStackMod == tmpMod &&
tmpStack[tmpStack.length-1] > tmp))
{
input.push(tmpStack[tmpStack.length-1]);
tmpStack.pop();
}
else
break ;
}
tmpStack.push(tmp);
}
while (tmpStack.length!=0)
{
input.push(tmpStack[tmpStack.length-1]);
tmpStack.pop();
}
while (input.length!=0)
{
document.write(input.pop() + " " );
}
}
let input =[];
input.push(10);
input.push(3);
input.push(2);
input.push(6);
input.push(12);
let k = 4;
sortStack(input, k);
</script>
|
Time Complexity: O(n2) where n is the total number of integers in the given stack.
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...