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.
Create another array which is double the size of the original array, such that the elements of this new array (copy array) are just the elements of the original array repeated twice in the same sequence. Example, if the original array is 1 4 1, then the copy array is 1 4 1 1 4 1.
Now, iterate through the copy array and find hamming distance with every shift (or rotation). So we check 4 1 1, 1 1 4, 1 4 1, choose the output for which the hamming distance is maximum.
Below is the implementation of above approach:
Time Complexity : O(n*n)
- Find maximum distance between any city and station
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Left Rotation and Right Rotation of a String
- Find the Rotation Count in Rotated Sorted array
- Maximum subsequence sum such that all elements are K distance apart
- Pair with given sum and maximum shortest distance from end
- Minimum distance between two occurrences of maximum
- Maximum distance between two unequal elements
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array
- Maximum distance between two occurrences of same element in array
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Find the minimum distance between two numbers
- Find the distance between two person after reconstruction of queue
- Find distance of nodes from root in a tree for multiple queries
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m