根据其值的模数及其频率对数组进行排序
Given an array arr containing N positive integers, sort them according to the increasing modulus of their values with their frequencies.
例子:
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
方法:要解决这个问题,将每个数字的频率存储在地图中,然后创建一个自定义比较器,它将进行排序。该比较函数将接受两个整数值,例如A和B作为参数,并且该比较器中对数组进行排序的条件是:
(A % frequency(A)) > (B % frequency(B))
下面是上述方法的实现。
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& arr)
{
// Map to store
// the frequencies of each number
unordered_map 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 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 freq = new Dictionary();
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
5 9 9 2 2 2 8 8 8
时间复杂度: O(N)
辅助空间: O(N)