C++ Program to Maximize count of corresponding same elements in given Arrays by Rotation Last Updated : 25 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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, 9, 5, 6 } Output: 5 Explanation: By performing cyclic left shift on array arr1[] by 1. Updated array arr1[] = {7, 3, 9, 5, 6}. This rotation contains a maximum number of equal elements between array arr1[] and arr2[].Input: arr1[] = {1, 3, 2, 4}, arr2[] = {4, 2, 3, 1} Output: 2 Explanation: By performing cyclic left shift on array arr1[] by 1. Updated array arr1[] = {3, 2, 4, 1} This rotation contains a maximum number of equal elements between array arr1[] and arr2[]. Approach: This problem can be solved using Greedy Approach. Below are the steps: Store the position of all the elements of the array arr2[] in an array(say store[]).For each element in the array arr1[], do the following: Find the difference(say diff) between the position of the current element in arr2[] with the position in arr1[].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 map is the maximum number of equal elements after rotation on arr1[]. Below is the implementation of the above approach: C++ // C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function that prints maximum // equal elements void maximumEqual(int a[], int b[], int n) { // Vector to store the index // of elements of array b vector<int> store(1e5); // Storing the positions of // array B for (int i = 0; i < n; i++) { store[b[i]] = i + 1; } // frequency array to keep count // of elements with similar // difference in distances vector<int> ans(1e5); // Iterate through all element in arr1[] for (int i = 0; i < n; i++) { // Calculate number of // shift required to // make current element // equal int d = abs(store[a[i]] - (i + 1)); // If d is less than 0 if (store[a[i]] < i + 1) { d = n - d; } // Store the frequency // of current diff ans[d]++; } int finalans = 0; // Compute the maximum frequency // stored for (int i = 0; i < 1e5; i++) finalans = max(finalans, ans[i]); // Printing the maximum number // of equal elements cout << finalans << " "; } // Driver Code int main() { // Given two arrays int A[] = { 6, 7, 3, 9, 5 }; int B[] = { 7, 3, 9, 5, 6 }; int size = sizeof(A) / sizeof(A[0]); // Function Call maximumEqual(A, B, size); return 0; } Output: 5 Time Complexity: O(N) Auxiliary Space: O(N) Please refer complete article on Maximize count of corresponding same elements in given Arrays by Rotation for more details! Comment More infoAdvertise with us Next Article C++ Program to Maximize count of corresponding same elements in given Arrays by Rotation kartik Follow Improve Article Tags : Greedy C++ Programs C++ DSA Arrays rotation +2 More Practice Tags : CPPArraysGreedy Similar Reads C++ Program to 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 4 min read 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 Modify given array to a non-decreasing array by rotation Given an array arr[] of size N (consisting of duplicates), the task is to check if the given array can be converted to a non-decreasing array by rotating it. If it's not possible to do so, then print "No". Otherwise, print "Yes". Examples: Input: arr[] = {3, 4, 5, 1, 2}Output: YesExplanation:Â After 2 min read C++ Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types: If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array. Examples:Â Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2, 5 min read C++ Program to Maximize sum of diagonal of a matrix by rotating all rows or all columns Given a square matrix, mat[][] of dimensions N * N, the task is find the maximum sum of diagonal elements possible from the given matrix by rotating either all the rows or all the columns of the matrix by a positive integer. Examples: Input: mat[][] = { { 1, 1, 2 }, { 2, 1, 2 }, { 1, 2, 2 } }Output: 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 Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3}Â Output: 2Â Explanation:Â Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2}Â Output: 1Â Explanation:Â So 4 min read C++ Program to Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Expl 3 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 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 Like