📌  相关文章
📜  通过旋转最大化给定数组中相应相同元素的数量

📅  最后修改于: 2021-05-14 02:14:41             🧑  作者: Mango

给定两个N个整数的数组arr1 []arr2 [] ,数组arr1 []具有不同的元素。任务是通过对数组arr1 []进行循环左移或右移,找到给定数组中相应相同元素的最大数量。

例子:

方法:此问题可以使用贪婪方法解决。步骤如下:

  1. 将数组arr2 []的所有元素的位置存储在数组中(例如store [] )。
  2. 对于数组arr1 []中的每个元素,请执行以下操作:
    • 求出当前元素在arr2 []中的位置与arr1 []中的位置之间的差异(例如diff )。
    • 如果diff小于0,则将diff更新为(N – diff)
    • 将电流差diff的频率存储在映射中。
  3. 完成上述步骤后,映射中存储的最大频率是在arr1 []上旋转之后的相等元素的最大数量。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
  
// Function that prints maximum
// equal elements
void maximumEqual(int a[], int b[],
                  int n)
{
  
    // Vector to store the index
    // of elements of array b
    vector store(1e5);
  
    // Storing the positions of
    // array B
    for (int i = 0; i < n; i++) {
        store[b[i]] = i + 1;
    }
  
    // frequency array to keep count
    // of elements with similar
    // difference in distances
    vector ans(1e5);
  
    // Iterate through all element in arr1[]
    for (int i = 0; i < n; i++) {
  
        // Calculate number of
        // shift required to
        // make current element
        // equal
        int d = abs(store[a[i]]
                    - (i + 1));
  
        // If d is less than 0
        if (store[a[i]] < i + 1) {
            d = n - d;
        }
  
        // Store the frequency
        // of current diff
        ans[d]++;
    }
  
    int finalans = 0;
  
    // Compute the maximum frequency
    // stored
    for (int i = 0; i < 1e5; i++)
        finalans = max(finalans,
                       ans[i]);
  
    // Printing the maximum number
    // of equal elements
    cout << finalans << "\n";
}
  
// Driver Code
int main()
{
    // Given two arrays
    int A[] = { 6, 7, 3, 9, 5 };
    int B[] = { 7, 3, 9, 5, 6 };
  
    int size = sizeof(A) / sizeof(A[0]);
  
    // Function Call
    maximumEqual(A, B, size);
    return 0;
}


Java
// Java program of the above approach
import java.util.*;
class GFG{
  
// Function that prints maximum
// equal elements
static void maximumEqual(int a[], 
                         int b[], int n)
{
  
    // Vector to store the index
    // of elements of array b
    int store[] = new int[(int) 1e5];
  
    // Storing the positions of
    // array B
    for (int i = 0; i < n; i++) 
    {
        store[b[i]] = i + 1;
    }
  
    // frequency array to keep count
    // of elements with similar
    // difference in distances
    int ans[] = new int[(int) 1e5];
  
    // Iterate through all element in arr1[]
    for (int i = 0; i < n; i++)
    {
  
        // Calculate number of
        // shift required to
        // make current element
        // equal
        int d = Math.abs(store[a[i]] - (i + 1));
  
        // If d is less than 0
        if (store[a[i]] < i + 1) 
        {
            d = n - d;
        }
  
        // Store the frequency
        // of current diff
        ans[d]++;
    }
  
    int finalans = 0;
  
    // Compute the maximum frequency
    // stored
    for (int i = 0; i < 1e5; i++)
        finalans = Math.max(finalans,
                            ans[i]);
  
    // Printing the maximum number
    // of equal elements
    System.out.print(finalans + "\n");
}
  
// Driver Code
public static void main(String[] args)
{
    // Given two arrays
    int A[] = { 6, 7, 3, 9, 5 };
    int B[] = { 7, 3, 9, 5, 6 };
  
    int size = A.length;
  
    // Function Call
    maximumEqual(A, B, size);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program for the above approach
  
# Function that prints maximum
# equal elements
def maximumEqual(a, b, n):
  
    # List to store the index
    # of elements of array b
    store = [0] * 10 ** 5
      
    # Storing the positions of
    # array B
    for i in range(n):
        store[b[i]] = i + 1
  
    # Frequency array to keep count
    # of elements with similar
    # difference in distances 
    ans = [0] * 10 ** 5
  
    # Iterate through all element 
    # in arr1[]
    for i in range(n):
  
        # Calculate number of shift 
        # required to make current 
        # element equal
        d = abs(store[a[i]] - (i + 1))
  
        # If d is less than 0
        if (store[a[i]] < i + 1):
            d = n - d
  
        # Store the frequency
        # of current diff
        ans[d] += 1
          
    finalans = 0
  
    # Compute the maximum frequency
    # stored
    for i in range(10 ** 5):
        finalans = max(finalans, ans[i])
  
    # Printing the maximum number
    # of equal elements
    print(finalans)
  
# Driver Code
if __name__ == '__main__':
  
    # Given two arrays
    A = [ 6, 7, 3, 9, 5 ]
    B = [ 7, 3, 9, 5, 6 ]
  
    size = len(A)
  
    # Function Call
    maximumEqual(A, B, size)
  
  
# This code is contributed by Shivam Singh


C#
// C# program of the above approach
using System;
class GFG{
  
// Function that prints maximum
// equal elements
static void maximumEqual(int[] a, 
                         int[] b, int n)
{
  
    // Vector to store the index
    // of elements of array b
    int[] store = new int[(int) 1e5];
  
    // Storing the positions of
    // array B
    for(int i = 0; i < n; i++) 
    {
       store[b[i]] = i + 1;
    }
  
    // Frequency array to keep count
    // of elements with similar
    // difference in distances
    int[] ans = new int[(int) 1e5];
  
    // Iterate through all element in arr1[]
    for(int i = 0; i < n; i++)
    {
         
       // Calculate number of
       // shift required to
       // make current element
       // equal
       int d = Math.Abs(store[a[i]] - (i + 1));
         
       // If d is less than 0
       if (store[a[i]] < i + 1) 
       {
           d = n - d;
       }
         
       // Store the frequency
       // of current diff
       ans[d]++;
    }
      
    int finalans = 0;
  
    // Compute the maximum frequency
    // stored
    for(int i = 0; i < 1e5; i++)
       finalans = Math.Max(finalans, ans[i]);
  
    // Printing the maximum number
    // of equal elements
    Console.Write(finalans + "\n");
}
  
// Driver Code
public static void Main()
{
      
    // Given two arrays
    int[]A = { 6, 7, 3, 9, 5 };
    int[]B = { 7, 3, 9, 5, 6 };
  
    int size = A.Length;
  
    // Function Call
    maximumEqual(A, B, size);
}
}
  
// This code is contributed by chitranayal


输出:
5

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