📌  相关文章
📜  找到最大汉明距离的旋转| 2套

📅  最后修改于: 2021-10-27 07:30:05             🧑  作者: Mango

给定一个整数数组arr[] 。创建一个新数组,它是给定数组的旋转,并找到两个数组之间的最大汉明距离。

注意:给定的输入可以有多个输出。

例子:

方法:使用Hashmap可以有效解决这个问题。请按照以下步骤解决问题

  • 遍历数组arr[]并将数组的值存储在HashMap 。根据兴趣,值可以是 List<> 或字符串。
  • 添加hashmap中的值,hashMap.add(arrvalue, list)。
  • 检查这个条件:
    • 当 hashMap.contains(arrValue) 然后 hashmap.get(arrayValue).append(index)。
  • 检查 hashmap 的大小,如果size == 1那么
    • 大汉明距离 = 0 。因为所有的值都是一样的。
  • 如果列表大小 = 1 ,则为每个键运行 for each 循环
    • 大汉明距离 = n 。因为所有的值都是不同的。
  • 创建一个大小为给定数组的1的数组,用于存储可能发生的类似事件。
  • 存储数组的所有索引后,将值存储在hashMap 中
  • 对于每个差异发现一个 += 1 。这种差异表示在相同位置具有相同值所需的旋转次数。
  • 找到数组中的最小值并获得索引 => 最小相同值 => 最大汉明距离。

下面是上述方法的实现:

C++
// Find a rotation with maximum hamming distance
#include
using namespace std;
 
// Function to return the min value
int getMinValue(vector numbers)
{
     
    // var to stor the min value
    int minValue = numbers[0];
     
    for(int i = 1; i < numbers.size(); i++)
    {
         
        // Condition to check if value is
        // lesser from the minValue or not
        if (numbers[i] < minValue)
        {
            minValue = numbers[i];
        }
    }
     
    // Return the minValue
    return minValue;
}
 
// Function to get the hamming distance
int maxHammingDistance(vector array)
{
    int n = array.size();
    vector repetitionsOnRotations(n - 1,0);
     
    // Create the map to store the key value pair
    map> indexesOfElements;
     
    for(int i = 0; i < n; i++)
    {
        int key = array[i];
         
        // Take empty list of integers
        // to store the integer
        vectorindexes;
         
        if (indexesOfElements.find(key) !=
            indexesOfElements.end())
        {
            indexes = indexesOfElements[key];
        }
        else
        {
            indexes.clear();
        }
         
        // Add the value in list index
        indexes.push_back(i);
        indexesOfElements[key] = indexes;
    }
         
    // Run the for loop and get the
    // value from hash map one at a time
    for(auto x : indexesOfElements)
    {
        vector indexes = x.second;
         
        for(int i = 0; i < indexes.size() - 1; i++)
        {
            for(int j = i + 1; j < indexes.size(); j++)
            {
                 
                // Store the diff into the variable
                int diff = indexes[i] - indexes[j];
                 
                // Check the condition if diff
                // is lees then zero or not
                if (diff < 0)
                {
                    repetitionsOnRotations[-diff - 1] += 1;
                    diff = n + diff;
                }
                repetitionsOnRotations += 1;
            }
        }
    }
     
    // Return the hamming distance
    return n - getMinValue(repetitionsOnRotations);
}
 
// Driver code
int main()
{
     
    // Example 1
    vector array{ 1, 4, 1 };
    int result1 = maxHammingDistance(array);
    cout << result1 << endl;
     
    // Example 2
    vector array2{ 2, 4, 8, 0 };
    int result2 = maxHammingDistance(array2);
    cout << result2 << endl;
}
 
// This code is contributed by ipg2016107


Java
// Find a rotation with maximum hamming distance
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to return the min value
    public static int getMinValue(int[] numbers)
    {
 
        // var to stor the min value
        int minValue = numbers[0];
 
        for (int i = 1; i < numbers.length; i++) {
 
            // Condition to check if value is
            // lesser from the minValue or not
            if (numbers[i] < minValue) {
                minValue = numbers[i];
            }
        }
 
        // Return the minValue
        return minValue;
    }
 
    // Function to get the hamming distance
    public static int maxHammingDistance(int[] array)
    {
 
        int n = array.length;
        int[] repetitionsOnRotations = new int[n - 1];
 
        // Create the map to store the key value pair
        Map > indexesOfElements
            = new HashMap<>();
 
        for (int i = 0; i < n; i++) {
 
            int key = array[i];
 
            // Take empty list of integers
            // to store the integer
            List indexes = null;
 
            if (indexesOfElements.containsKey(key)) {
                indexes = indexesOfElements.get(key);
            }
            else {
                indexes = new ArrayList<>();
            }
            // Add the value in list index
            indexes.add(i);
            indexesOfElements.put(key, indexes);
        }
 
        // Run the for loop and get the
        // value from hash map one at a time
        for (Map.Entry > keys :
             indexesOfElements.entrySet()) {
 
            List indexes = keys.getValue();
 
            for (int i = 0; i < indexes.size() - 1; i++) {
                for (int j = i + 1; j < indexes.size(); j++) {
 
                    // Store the diff into the variable
                    int diff
                        = indexes.get(i) - indexes.get(j);
 
                    // Check the condition if diff
                    // is lees then zero or not
                    if (diff < 0) {
                        repetitionsOnRotations[(-diff) - 1] += 1;
                        diff = n + diff;
                    }
                    repetitionsOnRotations += 1;
                }
            }
        }
 
        // Return the hamming distance
        return n - (getMinValue(repetitionsOnRotations));
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Example 1
        int[] array = { 1, 4, 1 };
        int result1 = GFG.maxHammingDistance(array);
        System.out.println(result1);
 
        // Example 2
        int[] array2 = { 2, 4, 8, 0 };
        int result2 = GFG.maxHammingDistance(array2);
        System.out.println(result2);
    }
}


Python3
# Find a rotation with maximum hamming distance
 
# Function to return the min value
def getMinValue(numbers):
 
    # var to stor the min value
    minValue = numbers[0]
 
    for i in range(1, len(numbers)):
 
        # Condition to check if value is
        # lesser from the minValue or not
        if (numbers[i] < minValue):
            minValue = numbers[i]
 
    # Return the minValue
    return minValue
 
 
# Function to get the hamming distance
def maxHammingDistance(array):
    n = len(array)
    repetitionsOnRotations = [0] * (n - 1)
 
    # Create the map to store the key value pair
    indexesOfElements = {}
 
    for i in range(n):
        key = array[i]
 
        # Take empty list of integers
        # to store the integer
        indexes = []
 
        if (key in indexesOfElements):
            indexes = indexesOfElements[key]
        else:
            indexes = []
 
        # Add the value in list index
        indexes.append(i)
        indexesOfElements[key] = indexes
 
    # Run the for loop and get the
    # value from hash map one at a time
    for indexes in indexesOfElements.values():
 
 
        for i in range(len(indexes) - 1):
            for j in range(i + 1, len(indexes)):
 
                # Store the diff into the variable
                diff = indexes[i] - indexes[j]
 
                # Check the condition if diff
                # is lees then zero or not
                if (diff < 0):
                    repetitionsOnRotations[-diff - 1] += 1
                    diff = n + diff
 
                repetitionsOnRotations += 1
 
    # Return the hamming distance
    return n - getMinValue(repetitionsOnRotations)
 
 
# Driver code
 
# Example 1
array = [1, 4, 1]
result1 = maxHammingDistance(array)
print(result1)
 
# Example 2
array2 = [2, 4, 8, 0]
result2 = maxHammingDistance(array2)
print(result2)
 
# This code is contributed by _saurabh_jaiswal


C#
// Find a rotation with maximum hamming distance
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to return the min value
public static int getMinValue(int[] numbers)
{
     
    // var to stor the min value
    int minValue = numbers[0];
 
    for(int i = 1; i < numbers.Length; i++)
    {
         
        // Condition to check if value is
        // lesser from the minValue or not
        if (numbers[i] < minValue)
        {
            minValue = numbers[i];
        }
    }
     
    // Return the minValue
    return minValue;
}
 
// Function to get the hamming distance
public static int maxHammingDistance(int[] array)
{
    int n = array.Length;
    int[] repetitionsOnRotations = new int[n - 1];
 
    // Create the map to store the key value pair
    Dictionary> indexesOfElements = new Dictionary>();
 
    for(int i = 0; i < n; i++)
    {
        int key = array[i];
 
        // Take empty list of integers
        // to store the integer
        List indexes = null;
 
        if (indexesOfElements.ContainsKey(key))
        {
            indexes = indexesOfElements[key];
        }
        else
        {
            indexes = new List();
        }
         
        // Add the value in list index
        indexes.Add(i);
        if (!indexesOfElements.ContainsKey(key))
            indexesOfElements.Add(key, indexes);
    }
 
    // Run the for loop and get the
    // value from hash map one at a time
    foreach(KeyValuePair> keys in indexesOfElements)
    {
        List indexes = keys.Value;
 
        for(int i = 0; i < indexes.Count - 1; i++)
        {
            for(int j = i + 1; j < indexes.Count; j++)
            {
                 
                // Store the diff into the variable
                int diff = indexes[i] - indexes[j];
 
                // Check the condition if diff
                // is lees then zero or not
                if (diff < 0)
                {
                    repetitionsOnRotations[(-diff) - 1] += 1;
                    diff = n + diff;
                }
                repetitionsOnRotations += 1;
            }
        }
    }
 
    // Return the hamming distance
    return n - (getMinValue(repetitionsOnRotations));
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Example 1
    int[] array = { 1, 4, 1 };
    int result1 = GFG.maxHammingDistance(array);
    Console.WriteLine(result1);
 
    // Example 2
    int[] array2 = { 2, 4, 8, 0 };
    int result2 = GFG.maxHammingDistance(array2);
    Console.WriteLine(result2);
}
}
 
// This code is contributed by umadevi9616


Javascript


输出:
2
4

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程