给定两个数组A[]和B[]由N 个正整数和一个整数M 组成,任务是找到X的最小值,使得操作(A[i] + X) % M对数组A 的每个元素执行[]导致形成一个数组,其元素频率与另一个给定数组B[] 中的元素频率相同。
例子:
Input: N = 4, M = 3, A[] = {0, 0, 2, 1}, B[] = {2, 0, 1, 1}
Output: 1
Explanation:
Modifying the given array A[] to { (0+1)%3, (0+1)%3, (2+1)%3, (1+1)%3 }
= { 1%3, 1%3, 3%3, 2%3 },
= { 1, 1, 0, 2 }, which is equivalent to B[] in terms of frequency of distinct elements.
Input: N = 5, M = 10, A[] = {0, 0, 0, 1, 2}, B[] = {2, 1, 0, 0, 0}
Output: 0
Explanation:
Frequency of elements in both the arrays are already equal.
方法:这个问题可以通过使用贪心方法来解决。请按照以下步骤操作:
- X至少有一个可能的值,使得对于每个索引i , ( A[i] + X ) % M = B[0] 。
- 找出将A[] 的每个元素转换为B[]的第一个元素的X 的所有可能值。
- 检查这些可能的X值是否满足B[]的其他剩余值。
- 如果有多个答案,取X的最小值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Utility function to find
// the answer
int moduloEquality(int A[], int B[],
int n, int m)
{
// Stores the frequencies of
// array elements
map mapA, mapB;
for (int i = 0; i < n; i++) {
mapA[A[i]]++;
mapB[B[i]]++;
}
// Stores the possible values
// of X
set possibleValues;
int FirstElement = B[0];
for (int i = 0; i < n; i++) {
int cur = A[i];
// Generate possible positive
// values of X
possibleValues
.insert(
cur > FirstElement
? m - cur + FirstElement
: FirstElement - cur);
}
// Initialize answer
// to MAX value
int ans = INT_MAX;
for (auto it :
possibleValues) {
// Flag to check if the
// current element of the
// set can be considered
bool possible = true;
for (auto it2 : mapA) {
// If the frequency of an element
// in A[] is not equal to that
// in B[] after the operation
if (it2.second
!= mapB[(it2.first + it) % m]) {
// Current set element
// cannot be considered
possible = false;
break;
}
}
// Update minimum value of X
if (possible) {
ans = min(ans, it);
}
}
return ans;
}
// Driver Code
int main()
{
int n = 4;
int m = 3;
int A[] = { 0, 0, 2, 1 };
int B[] = { 2, 0, 1, 1 };
cout << moduloEquality(A, B, n, m)
<< endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Utility function to find
// the answer
static int moduloEquality(int A[], int B[],
int n, int m)
{
// Stores the frequencies of
// array elements
HashMap mapA = new HashMap();
HashMap mapB = new HashMap();
for(int i = 0; i < n; i++)
{
if (mapA.containsKey(A[i]))
{
mapA.put(A[i], mapA.get(A[i]) + 1);
}
else
{
mapA.put(A[i], 1);
}
if (mapB.containsKey(B[i]))
{
mapB.put(B[i], mapB.get(B[i]) + 1);
}
else
{
mapB.put(B[i], 1);
}
}
// Stores the possible values
// of X
HashSet possibleValues = new HashSet();
int FirstElement = B[0];
for(int i = 0; i < n; i++)
{
int cur = A[i];
// Generate possible positive
// values of X
possibleValues.add(cur > FirstElement ?
m - cur + FirstElement :
FirstElement - cur);
}
// Initialize answer
// to MAX value
int ans = Integer.MAX_VALUE;
for(int it : possibleValues)
{
// Flag to check if the
// current element of the
// set can be considered
boolean possible = true;
for(Map.Entry it2 : mapA.entrySet())
{
// If the frequency of an element
// in A[] is not equal to that
// in B[] after the operation
if (it2.getValue() !=
mapB.get((it2.getKey() + it) % m))
{
// Current set element
// cannot be considered
possible = false;
break;
}
}
// Update minimum value of X
if (possible)
{
ans = Math.min(ans, it);
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
int m = 3;
int A[] = { 0, 0, 2, 1 };
int B[] = { 2, 0, 1, 1 };
System.out.print(moduloEquality(A, B, n, m) + "\n");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
import sys
from collections import defaultdict
# Utility function to find
# the answer
def moduloEquality(A, B, n, m):
# Stores the frequencies of
# array elements
mapA = defaultdict(int)
mapB = defaultdict(int)
for i in range(n):
mapA[A[i]] += 1
mapB[B[i]] += 1
# Stores the possible values
# of X
possibleValues = set()
FirstElement = B[0]
for i in range(n):
cur = A[i]
# Generate possible positive
# values of X
if cur > FirstElement:
possibleValues.add(m - cur + FirstElement)
else:
possibleValues.add(FirstElement - cur)
# Initialize answer
# to MAX value
ans = sys.maxsize
for it in possibleValues:
# Flag to check if the
# current element of the
# set can be considered
possible = True
for it2 in mapA:
# If the frequency of an element
# in A[] is not equal to that
# in B[] after the operation
if (mapA[it2] !=
mapB[(it2 + it) % m]):
# Current set element
# cannot be considered
possible = False
break
# Update minimum value of X
if (possible):
ans = min(ans, it)
return ans
# Driver Code
if __name__ == "__main__":
n = 4
m = 3
A = [ 0, 0, 2, 1 ]
B = [ 2, 0, 1, 1 ]
print(moduloEquality(A, B, n, m))
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Utility function to find
// the answer
static int moduloEquality(int[] A, int[] B,
int n, int m)
{
// Stores the frequencies of
// array elements
Dictionary mapA = new Dictionary();
Dictionary mapB = new Dictionary();
for(int i = 0; i < n; i++)
{
if (mapA.ContainsKey(A[i]))
{
mapA[A[i]] = mapA[A[i]] + 1;
}
else
{
mapA.Add(A[i], 1);
}
if (mapB.ContainsKey(B[i]))
{
mapB[B[i]] = mapB[B[i]] + 1;
}
else
{
mapB.Add(B[i], 1);
}
}
// Stores the possible values
// of X
HashSet possibleValues = new HashSet();
int FirstElement = B[0];
for(int i = 0; i < n; i++)
{
int cur = A[i];
// Generate possible positive
// values of X
possibleValues.Add(cur > FirstElement ?
m - cur + FirstElement :
FirstElement - cur);
}
// Initialize answer
// to MAX value
int ans = Int32.MaxValue;
foreach(int it in possibleValues)
{
// Flag to check if the
// current element of the
// set can be considered
bool possible = true;
foreach(KeyValuePair it2 in mapA)
{
// If the frequency of an element
// in A[] is not equal to that
// in B[] after the operation
if (it2.Value != mapB[(it2.Key + it) % m])
{
// Current set element
// cannot be considered
possible = false;
break;
}
}
// Update minimum value of X
if (possible)
{
ans = Math.Min(ans, it);
}
}
return ans;
}
// Driver code
static void Main()
{
int n = 4;
int m = 3;
int[] A = { 0, 0, 2, 1 };
int[] B = { 2, 0, 1, 1 };
Console.WriteLine(moduloEquality(A, B, n, m));
}
}
// This code is contributed by divyeshrabadiya07
Javascript
1
时间复杂度: O(N 2 )
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。