# Sort Array according to modulus of their values with their frequencies

• Last Updated : 04 Apr, 2022

Given an array arr containing N positive integers, sort them according to the increasing modulus of their values with their frequencies.

Example:

Input: arr[]={1, 1, 5, 3, 2, 3, 3, 3, 4, 5, 4, 5}
Output: 2 4 4 1 1 5 5 5 3 3 3 3
Explanation:
The elements are sorted in the following order:
2 % frequency(2) = 2 % 1 = 0
4 % frequency(4) = 4 % 2 = 0
1 % frequency(1) = 1 % 2 = 1
5 % frequency(5) = 5 % 3 = 2
3 % frequency(4) = 3 % 4 = 3

Input: arr[]={2, 9, 8, 2, 8, 9, 2, 8, 5}
Output: 5 9 9 2 2 2 8 8 8

Approach: To solve this question, store the frequencies of each number in a map and then create a custom comparator, which will do the sorting. That compare function will accept two integer values, say A and B as parameters and the condition in that comparator to sort the array is:

(A % frequency(A)) > (B % frequency(B))

Below is the implementation of the above approach.

## C++

 `// C++ code for the above approach` `#include ``using` `namespace` `std;` `// Function to sort the numbers in an array``// according to the modulus of their values``// with their frequencies``void` `customSort(vector<``int``>& arr)``{` `    ``// Map to store``    ``// the frequencies of each number``    ``unordered_map<``int``, ``int``> freq;``    ``for` `(``auto` `x : arr) {``        ``freq[x]++;``    ``}` `    ``// Sorting them using``    ``// a custom comparator``    ``sort(arr.begin(), arr.end(),``         ``[&](``int` `A, ``int` `B) {``             ``if` `(A % freq[A] == B % freq[B]) {``                 ``return` `A < B;``             ``}``             ``return` `A % freq[A] < B % freq[B];``         ``});` `    ``// Printing the numbers``    ``for` `(``auto` `x : arr) {``        ``cout << x << ``' '``;``    ``}``}` `// Driver Code``int` `main()``{``    ``vector<``int``> arr = { 2, 9, 8, 2, 8,``                        ``9, 2, 8, 5 };``    ``customSort(arr);``}`

## Java

 `// Java code for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to sort the numbers in an array``// according to the modulus of their values``// with their frequencies``static` `void` `customSort(Integer []arr)``{` `    ``// Map to store``    ``// the frequencies of each number``    ``HashMap freq = ``new` `HashMap();``    ``for` `(``int` `x : arr) {``        ``if``(freq.containsKey(x)){``            ``freq.put(x, freq.get(x)+``1``);``        ``}``        ``else``{``            ``freq.put(x, ``1``);``        ``}``    ``}` `    ``// Sorting them using``    ``// a custom comparator``    ``Arrays.sort(arr, ``new` `Comparator(){`` ` `        ``@Override``        ``public` `int` `compare(Integer a, Integer b)``        ``{``            ``// If both are odd or even``            ``// then sorting in increasing order``            ``if` `((a % freq.get(a)) == (b % freq.get(b))) {``                ``return` `a < b?-``1``:``1``;``            ``}`` ` `            ``// Sorting on the basis of last bit if``            ``// if one is odd and the other one is even``            ``return` `((a % freq.get(a)) < (b % freq.get(b)))?-``1``:``1``;``        ``}``         ` `    ``});``    ``// Printing the numbers``    ``for` `(``int` `x : arr) {``        ``System.out.print(x +``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``Integer []arr = { ``2``, ``9``, ``8``, ``2``, ``8``,``                        ``9``, ``2``, ``8``, ``5` `};``    ``customSort(arr);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 code for the above approach` `# Function to sort the numbers in an array``# according to the modulus of their values``# with their frequencies``from` `collections ``import` `OrderedDict``from` `filecmp ``import` `cmp``from` `functools ``import` `cmp_to_key` `freq ``=` `{}` `def` `custom_sort(A, B):``    ` `    ``global` `freq` `    ``if` `(A ``%` `freq[A] ``=``=` `B ``%` `freq[B]):``        ``return` `A ``-` `B` `    ``return` `((A ``%` `freq[A]) ``-` `(B ``%` `freq[B]))` `def` `arr_sort(a,b):``    ``return` `a[``0``] ``-` `b[``0``]` `def` `customSort(arr):` `    ``global` `freq``    ` `    ``# Map to store``    ``# the frequencies of each number``    ``for` `x ``in` `arr :``        ``if` `x ``in` `freq:``            ``freq[x] ``=` `freq[x] ``+` `1``        ``else``:``            ``freq[x] ``=` `1` `    ``freq ``=` `OrderedDict(``sorted``(freq.items(),key ``=` `cmp_to_key(arr_sort)))` `    ``# Sorting them using``    ``# a custom comparator``    ``arr.sort(key ``=` `cmp_to_key(custom_sort))` `    ``# Printing the numbers``    ``for` `x ``in` `arr:``        ``print``(x,end ``=` `' '``)` `# Driver Code``arr ``=` `[``2``, ``9``, ``8``, ``2``, ``8``, ``9``, ``2``, ``8``, ``5``]``customSort(arr)` `# This code is contributed by shinjanpatra`

## C#

 `// C# code for the above approach` `using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `    ``// Function to sort the numbers in an array``    ``// according to the modulus of their values``    ``// with their frequencies``    ``static` `void` `customSort(``int``[] arr)``    ``{` `        ``// Map to store``        ``// the frequencies of each number``        ``Dictionary<``int``, ``int``> freq = ``new` `Dictionary<``int``, ``int``>();``        ``foreach` `(``int` `x ``in` `arr)``        ``{``            ``if` `(freq.ContainsKey(x))``            ``{``                ``freq[x] += 1;``            ``}``            ``else``            ``{``                ``freq[x] = 1;``            ``}``        ``}` `        ``// Sorting them using``        ``// a custom comparator``        ``Array.Sort(arr, (a, b)``             ``=>``        ``{``            ``// If both are odd or even``            ``// then sorting in increasing order``            ``if` `((a % freq[a]) == (b % freq[b]))``            ``{``                ``return` `a < b ? -1 : 1;``            ``}` `            ``// Sorting on the basis of last bit if``            ``// if one is odd and the other one is even``            ``return` `((a % freq[a]) < (b % freq[b])) ? -1 : 1;``        ``});``        ``// Printing the numbers` `        ``foreach` `(``int` `x ``in` `arr)``        ``{``            ``Console.Write(x + ``" "``);``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = { 2, 9, 8, 2, 8,``                        ``9, 2, 8, 5 };``        ``customSort(arr);``    ``}``}` `// This code is contributed by Saurabh Jaiswal`

## Javascript

 ``

Output
`5 9 9 2 2 2 8 8 8 `

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up