📌  相关文章
📜  要在第一个数组模M中添加的最小数字,以使两个数组的频率相等

📅  最后修改于: 2021-04-25 00:31:40             🧑  作者: Mango

给定由N个正整数和整数M组成的两个数组A []B [] ,任务是找到X的最小值,以便对数组A的每个元素执行(A [i] + X)%M的运算[]导致形成一个数组,该数组的元素频率与另一个给定数组B []中的元素频率相同。

例子:

方法:此问题可以通过使用贪婪方法解决。请按照以下步骤操作:

  • 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 frequncies 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 posible = 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
                posible = false;
                break;
            }
        }
 
        // Update minimum value of X
        if (posible) {
            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 frequncies 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 posible = 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
                posible = false;
                break;
            }
        }
 
        // Update minimum value of X
        if (posible)
        {
            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 frequncies 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
        posible = 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
                posible = False
                break
 
        # Update minimum value of X
        if (posible):
            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 frequncies 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 posible = 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
                posible = false;
                break;
            }
        }
  
        // Update minimum value of X
        if (posible)
        {
            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


输出:
1




时间复杂度: O(N 2 )
辅助空间: O(N)