# Sort the given stack elements based on their modulo with K

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++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to sort the stack using ` `// another stack based on the ` `// values of elements modulo k ` `void` `sortStack(stack<` `int` `>& input, ` `int` `k) ` `{ ` ` ` `stack<` `int` `> tmpStack; ` ` ` ` ` `while` `(!input.empty()) { ` ` ` ` ` `// Pop out the first element ` ` ` `int` `tmp = input.top(); ` ` ` `input.pop(); ` ` ` ` ` `// While temporary stack is not empty ` ` ` `while` `(!tmpStack.empty()) { ` ` ` `int` `tmpStackMod = tmpStack.top() % k; ` ` ` `int` `tmpMod = tmp % k; ` ` ` ` ` `// The top of the stack modulo k is ` ` ` `// greater than (temp & k) or if they ` ` ` `// are equal then compare the values ` ` ` `if` `((tmpStackMod > tmpMod) ` ` ` `|| (tmpStackMod == tmpMod ` ` ` `&& tmpStack.top() > tmp)) { ` ` ` ` ` `// Pop from temporary stack and push ` ` ` `// it to the input stack ` ` ` `input.push(tmpStack.top()); ` ` ` `tmpStack.pop(); ` ` ` `} ` ` ` `else` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Push temp in tempory of stack ` ` ` `tmpStack.push(tmp); ` ` ` `} ` ` ` ` ` `// Push all the elements in the original ` ` ` `// stack to get the ascending order ` ` ` `while` `(!tmpStack.empty()) { ` ` ` `input.push(tmpStack.top()); ` ` ` `tmpStack.pop(); ` ` ` `} ` ` ` ` ` `// Print the sorted elements ` ` ` `while` `(!input.empty()) { ` ` ` `cout << input.top() << ` `" "` `; ` ` ` `input.pop(); ` ` ` `} ` `} ` ` ` `// Driver code ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

12 2 6 10 3

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Sort elements by modulo with K
- Sort elements of array whose modulo with K yields P
- Sort an Array based on the absolute difference of adjacent elements
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Introduction of Stack based CPU Organization
- Growable array based stack
- Sort a stack using a temporary stack
- Sort the character array based on ASCII % N
- Sort an array of strings based on the given order
- Sort an array in descending order based on the sum of its occurrence
- Sort an array of strings based on the frequency of good words in them
- Sort the strings based on the numbers of matchsticks required to represent them
- Sort numbers based on count of letters required to represent them in words
- Sort a stack using recursion
- Sort string of characters using Stack
- Add elements in start to sort the array | Variation of Stalin Sort
- Insertion sort to sort even and odd positioned elements in different orders
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Maximize removal of adjacent array elements based on their absolute value
- Delete all even elements from a stack

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.