Sort Array according to modulus of their values with their frequencies
Last Updated :
28 Mar, 2023
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))
Algorithm:
- Create an unordered map to store the frequencies of each number in the array.
- Sort the array using a custom comparator that compares the modulus of the values of two elements in the array with their respective frequencies.
- If the modulus is the same, compare the values of the elements and return true if the first element is smaller than the second element.
- If the modulus is not the same, compare the modulus of the two elements and return true if the modulus of the first element is smaller than the modulus of the second element.
- Print the sorted array.
Pseudocode:
function customSort(array):
freq_map = empty unordered map
for element in array:
increment the frequency of element in freq_map
sort array using a custom comparator:
if modulus of A with freq[A] is equal to modulus of B with freq[B]:
return A < B
else:
return modulus of A with freq[A] < modulus of B with freq[B]
print the sorted array
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void customSort(vector< int >& arr)
{
unordered_map< int , int > freq;
for ( auto x : arr) {
freq[x]++;
}
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];
});
for ( auto x : arr) {
cout << x << ' ' ;
}
}
int main()
{
vector< int > arr = { 2, 9, 8, 2, 8,
9, 2, 8, 5 };
customSort(arr);
}
|
Java
import java.util.*;
class GFG{
static void customSort(Integer []arr)
{
HashMap<Integer,Integer> freq = new HashMap<Integer,Integer>();
for ( int x : arr) {
if (freq.containsKey(x)){
freq.put(x, freq.get(x)+ 1 );
}
else {
freq.put(x, 1 );
}
}
Arrays.sort(arr, new Comparator<Integer>(){
@Override
public int compare(Integer a, Integer b)
{
if ((a % freq.get(a)) == (b % freq.get(b))) {
return a < b?- 1 : 1 ;
}
return ((a % freq.get(a)) < (b % freq.get(b)))?- 1 : 1 ;
}
});
for ( int x : arr) {
System.out.print(x + " " );
}
}
public static void main(String[] args)
{
Integer []arr = { 2 , 9 , 8 , 2 , 8 ,
9 , 2 , 8 , 5 };
customSort(arr);
}
}
|
Python3
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
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)))
arr.sort(key = cmp_to_key(custom_sort))
for x in arr:
print (x,end = ' ' )
arr = [ 2 , 9 , 8 , 2 , 8 , 9 , 2 , 8 , 5 ]
customSort(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void customSort( int [] arr)
{
Dictionary< int , int > freq = new Dictionary< int , int >();
foreach ( int x in arr)
{
if (freq.ContainsKey(x))
{
freq[x] += 1;
}
else
{
freq[x] = 1;
}
}
Array.Sort(arr, (a, b)
=>
{
if ((a % freq[a]) == (b % freq[b]))
{
return a < b ? -1 : 1;
}
return ((a % freq[a]) < (b % freq[b])) ? -1 : 1;
});
foreach ( int x in arr)
{
Console.Write(x + " " );
}
}
public static void Main()
{
int [] arr = { 2, 9, 8, 2, 8,
9, 2, 8, 5 };
customSort(arr);
}
}
|
Javascript
<script>
function customSort(arr) {
let freq = new Map();
for (x of arr) {
if (freq.has(x)) {
freq.set(x, freq.get(x) + 1)
} else {
freq.set(x, 1)
}
}
freq = new Map([...freq].sort((a, b) => a[0] - b[0]));
arr.sort((A, B) => {
if (A % freq.get(A) == B % freq.get(B)) {
return A - B;
}
return ((A % freq.get(A)) - (B % freq.get(B)));
});
for (x of arr) {
document.write(x + ' ' );
}
}
let arr = [2, 9, 8, 2, 8, 9, 2, 8, 5];
customSort(arr);
</script>
|
Time Complexity: O(N*log2N) as sorting takes N*log2N time.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...