C++ Program to Maximize count of corresponding same elements in given permutations using cyclic rotations Last Updated : 18 Aug, 2023 Comments Improve Suggest changes Like Article Like Report 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: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to maximize the matching // pairs between two permutation // using left and right rotation int maximumMatchingPairs(int perm1[], int perm2[], int n) { // Left array store distance of element // from left side and right array store // distance of element from right side int left[n], right[n]; // Map to store index of elements map<int, int> mp1, mp2; for (int i = 0; i < n; i++) { mp1[perm1[i]] = i; } for (int j = 0; j < n; j++) { mp2[perm2[j]] = j; } for (int i = 0; i < n; i++) { // idx1 is index of element // in first permutation // idx2 is index of element // in second permutation int idx2 = mp2[perm1[i]]; int idx1 = i; if (idx1 == idx2) { // If element if present on same // index on both permutations then // distance is zero left[i] = 0; right[i] = 0; } else if (idx1 < idx2) { // Calculate distance from left // and right side left[i] = (n - (idx2 - idx1)); right[i] = (idx2 - idx1); } else { // Calculate distance from left // and right side left[i] = (idx1 - idx2); right[i] = (n - (idx1 - idx2)); } } // Maps to store frequencies of elements // present in left and right arrays map<int, int> freq1, freq2; for (int i = 0; i < n; i++) { freq1[left[i]]++; freq2[right[i]]++; } int ans = 0; for (int i = 0; i < n; i++) { // Find maximum frequency ans = max(ans, max(freq1[left[i]], freq2[right[i]])); } // Return the result return ans; } // Driver Code int main() { // Given permutations P1 and P2 int P1[] = { 5, 4, 3, 2, 1 }; int P2[] = { 1, 2, 3, 4, 5 }; int n = sizeof(P1) / sizeof(P1[0]); // Function Call cout << maximumMatchingPairs(P1, P2, n); return 0; } Output: 1 Time Complexity: O(N) Auxiliary Space: O(N), since N extra space has been taken Please refer complete article on Maximize count of corresponding same elements in given permutations using cyclic rotations for more details! Comment More infoAdvertise with us Next Article C++ Program to Maximize count of corresponding same elements in given permutations using cyclic rotations kartik Follow Improve Article Tags : Algorithms Searching Competitive Programming C++ Programs C++ DSA Arrays permutation rotation Natural Numbers +6 More Practice Tags : CPPAlgorithmsArrayspermutationSearching +1 More Similar Reads Maximize count of corresponding same elements in given permutations using cyclic rotations 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 11 min read C++ Program to Maximize count of corresponding same elements in given Arrays by Rotation Given two arrays arr1[] and arr2[] of N integers and array arr1[] has distinct elements. The task is to find the maximum count of corresponding same elements in the given arrays by performing cyclic left or right shift on array arr1[]. Examples:  Input: arr1[] = { 6, 7, 3, 9, 5 }, arr2[] = { 7, 3, 3 min read C++ Program to Maximum sum of i*arr[i] among all rotations of a given array Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1. Examples: Input: arr[] = {8, 3, 1, 2} Output: 29 Explanation: Lets look at all the rotations, {8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11 {3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 6 min read C++ Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i]. Examples :  Input: arr[] = {1, 20, 2, 10} Output: 72 We can get 72 by rotating array twice. {2, 10, 1, 20} 20*3 + 1*2 + 10*1 + 2*0 = 72 I 4 min read Iterative program to generate distinct Permutations of a String Given a string str, the task is to generate all the distinct permutations of the given string iteratively. Examples: Input: str = "bba" Output: abb bab bba Input: str = "abc" Output: abc acb bac bca cab cba Approach: The number of permutations for a string of length n are n!. The following algorithm 15+ min read C++ Program to Find Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N. Examples: Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765. Input: N = 7092Output: 9270Explanati 2 min read C++ Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 6 min read Count of distinct permutations of every possible length of given string Given a string S, the task is to count the distinct permutations of every possible length of the given string. Note: Repetition of characters is not allowed in the string. Input: S = âabcâOutput: 15Explanation:Possible Permutations of every length are:{âaâ, âbâ, âcâ, âabâ, âbcâ, âacâ, âbaâ, âcaâ, âc 5 min read C++ Program for Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end.Examples: Input : n = 2 arr[] = {"molzv", "lzvmo"} Output : 2 Explanation: In first string, we remove first element("m") from first 3 min read Sort permutation of N natural numbers using triple cyclic right swaps Given an array arr[] of size N which contains the permutations of the N natural numbers, the task is to sort the permutations of N natural numbers with the help of triple cyclic right swaps. Triple Cyclic Right Swaps: refers to the triple cyclic right shift in which - arr[i] -> arr[j] -> arr[k 11 min read Like