Given an array of n elements, create a new array which is a rotation of given array and hamming distance between both the arrays is maximum.
Hamming distance between two arrays or strings of equal length is the number of positions at which the corresponding character(elements) are different.
Note: There can be more than one output for the given input.
Input : 1 4 1 Output : 2 Explanation: Maximum hamming distance = 2. We get this hamming distance with 4 1 1 or 1 1 4 Input : N = 4 2 4 8 0 Output : 4 Explanation: Maximum hamming distance = 4 We get this hamming distance with 4 8 0 2. All the places can be occupied by another digit. Other solutions can be 8 0 2 4, 4 0 2 8 etc.
We can find the maximum hamming distance using a different approach by taking advantage of list-comprehension in python. In this method, we divide the job in 3 separate functions.
• hamming_distance(x : list, y : list): This method returns the hamming distance for two list passed as parameters. The idea is to count the positions at which elements are different at the same index in two lists x and y where x is the original array taken in input and y is one of it rotations. Initialize a variable count from 0. Run loop from starting index 0 to last index (n-1) where n is the length of the list. For each iteration check if element of x and element at index i (0<=i<=n-1) is same or not. If they are same, increment the counter. After loop is completed, return the count(by definition this is the hamming distance for given arrays or strings)
• rotate_by_one(arr : list): This method rotates the array (passed in argument ) in anti-clockwise direction by 1 position. For e.g. if array [1,1,4,4] is passed, this method returns [1,4,4,5,1]. The idea is to copy the 1st element of the array and save it in a variable (say x). Then iterate the array from 0 to n-2 and copy every i+1 th value at ith position. Now assign x to last index.
• max_hamming_distance(arr : list): This method finds the maximum hamming distance for a given array and it’s rotations. Follow below steps in this method. We copy this array in a new array (say a) and initialize a variable max. Now, after every n rotations we get the original array. So we need to find hamming distance for original array with it’s n-1 rotations and store the current maximum in a variable(say max). Run loop for n-1 iterations.