# Sort elements by modulo with K

Given an array **arr[]** of integers and an integer **K**. The task is to sort the elements of the given array in the increasing order of their modulo with **K**. If two numbers have the same remainder then smaller number should come first.

**Examples**:

Input:arr[] = {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 {0, 2, 2, 2, 3}.

Input:arr[] = {3, 4, 5, 10, 11, 1}, K = 3

Output:3 1 4 10 5 11

**Approach:**

- Create
**K**empty vectors. - Traverse the array from left to right and update the vectors such that the
**i**vector contains the elements that give^{th}**i**as the remainder when divided by**K**. - Sort all the vectors separately as all the elements that give the same modulo value with
**K**have to be sorted in ascending. - Now, starting from the first vector to the last vector and going from left to right in the vectors will give the elements in the required sorted order.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utility function to print the ` `// contents of an array ` `void` `printArr(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Function to sort the array elements ` `// based on their modulo with K ` `void` `sortWithRemainder(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Create K empty vectors ` ` ` `vector<` `int` `> v[k]; ` ` ` ` ` `// Update the vectors such that v[i] ` ` ` `// will contain all the elements ` ` ` `// that give remainder as i ` ` ` `// when divided by k ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `v[arr[i] % k].push_back(arr[i]); ` ` ` `} ` ` ` ` ` `// Sorting all the vectors separately ` ` ` `for` `(` `int` `i = 0; i < k; i++) ` ` ` `sort(v[i].begin(), v[i].end()); ` ` ` ` ` `// Replacing the elements in arr[] with ` ` ` `// the required modulo sorted elements ` ` ` `int` `j = 0; ` ` ` `for` `(` `int` `i = 0; i < k; i++) { ` ` ` ` ` `// Add all the elements of the ` ` ` `// current vector to the array ` ` ` `for` `(vector<` `int` `>::iterator it = v[i].begin(); ` ` ` `it != v[i].end(); it++) { ` ` ` ` ` `arr[j] = *it; ` ` ` `j++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the sorted array ` ` ` `printArr(arr, n); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 10, 7, 2, 6, 12, 3, 33, 46 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 4; ` ` ` ` ` `sortWithRemainder(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

12 33 2 6 10 46 3 7

**Time Complexity:** O(nlogn)

## Recommended Posts:

- Sort the given stack elements based on their modulo with K
- Sort elements of array whose modulo with K yields P
- Longest subarray with elements having equal modulo K
- Insertion sort to sort even and odd positioned elements in different orders
- Sort elements by frequency | Set 2
- Sort elements by frequency | Set 1
- Insertion Sort by Swapping Elements
- Sort elements by frequency | Set 5 (using Java Map)
- Sort 1 to N by swapping adjacent elements
- Sort even and odd placed elements in increasing order
- Sort an array containing two types of elements
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Python | Sort a List according to the Length of the Elements
- Sort elements on the basis of number of factors
- Sort elements of the array that occurs in between multiples of K

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.