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.
Input: P1 = [5 4 3 2 1], P2 = [1 2 3 4 5]
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]
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 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 optimised. The idea is for every element store the smaller distance between positions of this element from left and right sides in separate arrays. Hence the solution to the problem will be calculated as 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 above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
- Maximize count of corresponding same elements in given Arrays by Rotation
- Maximize count of distinct elements possible in an Array from the given operation
- Minimize count of array elements to be removed to maximize difference between any pair up to K
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Maximize a number considering permutations with values smaller than limit
- Number of cyclic elements in an array where we can jump according to value
- Count rotations of N which are Odd and Even
- Count all indices of cyclic regular parenthesis
- Count rotations which are divisible by 10
- Count rotations divisible by 8
- Count rotations divisible by 4
- Count of ways to travel a cyclic path in N steps in a Triangular Pyramid
- Count number of ways to convert string S to T by performing K cyclic shifts
- Count of rotations required to generate a sorted array
- Count rotations in sorted and rotated linked list
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Maximize sum of K elements in Array by taking only corner elements
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Maximize sum of topmost elements of S stacks by popping at most N elements
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
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 : chitranayal