Given two permutations P1 and P2 of numbers from 1 to N, the task is to find the maximum count of corresponding same elements in the given permutations by performing a cyclic left or right shift on P1.
Examples:
Input: P1 = [5 4 3 2 1], P2 = [1 2 3 4 5]
Output: 1
Explanation:
We have a matching pair at index 2 for element 3.
Input: P1 = [1 3 5 2 4 6], P2 = [1 5 2 4 3 6]
Output: 3
Explanation:
Cyclic shift of second permutation towards right would give 6 1 5 2 4 3, and we get a match of 5, 2, 4. Hence, the answer is 3 matching pairs.
Naive Approach: The naive approach is to check for every possible shift in both the left and right direction count the number of matching pairs by looping through all the permutations formed.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above naive approach can be optimized. The idea is for every element to store the smaller distance between positions of this element from the left and right sides in separate arrays. Hence, the solution to the problem will be calculated as the maximum frequency of an element from the two separated arrays. Below are the steps:
- Store the position of all the elements of the permutation P2 in an array(say store[]).
- For each element in the permutation P1, do the following:
- Find the difference(say diff) between the position of the current element in P2 with the position in P1.
- If diff is less than 0 then update diff to (N – diff).
- Store the frequency of current difference diff in a map.
- After the above steps, the maximum frequency stored in the map is the maximum number of equal elements after rotation on P1.
Below is the implementation of the above approach:
Python3
from collections import defaultdict
def maximumMatchingPairs(perm1, perm2, n):
left = [ 0 ] * n
right = [ 0 ] * n
mp1 = {}
mp2 = {}
for i in range (n):
mp1[perm1[i]] = i
for j in range (n):
mp2[perm2[j]] = j
for i in range (n):
idx2 = mp2[perm1[i]]
idx1 = i
if (idx1 = = idx2):
left[i] = 0
right[i] = 0
elif (idx1 < idx2):
left[i] = (n - (idx2 - idx1))
right[i] = (idx2 - idx1)
else :
left[i] = (idx1 - idx2)
right[i] = (n - (idx1 - idx2))
freq1 = defaultdict ( int )
freq2 = defaultdict ( int )
for i in range (n):
freq1[left[i]] + = 1
freq2[right[i]] + = 1
ans = 0
for i in range ( n):
ans = max (ans, max (freq1[left[i]],
freq2[right[i]]))
return ans
if __name__ = = "__main__" :
P1 = [ 5 , 4 , 3 , 2 , 1 ]
P2 = [ 1 , 2 , 3 , 4 , 5 ]
n = len (P1)
print (maximumMatchingPairs(P1, P2, n))
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Please refer complete article on Maximize count of corresponding same elements in given permutations using cyclic rotations for more details!
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Jan, 2022
Like Article
Save Article