Maximize count of corresponding same elements in given Arrays by Rotation
Last Updated :
24 Feb, 2022
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 << "\n";
}
// 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;
}
Java
// Java program of the above approach
import java.util.*;
class GFG{
// Function that prints maximum
// equal elements
static void maximumEqual(int a[],
int b[], int n)
{
// Vector to store the index
// of elements of array b
int store[] = new int[(int) 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
int ans[] = new int[(int) 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 = Math.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 = Math.max(finalans,
ans[i]);
// Printing the maximum number
// of equal elements
System.out.print(finalans + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given two arrays
int A[] = { 6, 7, 3, 9, 5 };
int B[] = { 7, 3, 9, 5, 6 };
int size = A.length;
// Function Call
maximumEqual(A, B, size);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program for the above approach
# Function that prints maximum
# equal elements
def maximumEqual(a, b, n):
# List to store the index
# of elements of array b
store = [0] * 10 ** 5
# Storing the positions of
# array B
for i in range(n):
store[b[i]] = i + 1
# Frequency array to keep count
# of elements with similar
# difference in distances
ans = [0] * 10 ** 5
# Iterate through all element
# in arr1[]
for i in range(n):
# Calculate number of shift
# required to make current
# element equal
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] += 1
finalans = 0
# Compute the maximum frequency
# stored
for i in range(10 ** 5):
finalans = max(finalans, ans[i])
# Printing the maximum number
# of equal elements
print(finalans)
# Driver Code
if __name__ == '__main__':
# Given two arrays
A = [ 6, 7, 3, 9, 5 ]
B = [ 7, 3, 9, 5, 6 ]
size = len(A)
# Function Call
maximumEqual(A, B, size)
# This code is contributed by Shivam Singh
C#
// C# program of the above approach
using System;
class GFG{
// Function that prints maximum
// equal elements
static void maximumEqual(int[] a,
int[] b, int n)
{
// Vector to store the index
// of elements of array b
int[] store = new int[(int) 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
int[] ans = new int[(int) 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 = Math.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 = Math.Max(finalans, ans[i]);
// Printing the maximum number
// of equal elements
Console.Write(finalans + "\n");
}
// Driver Code
public static void Main()
{
// Given two arrays
int[]A = { 6, 7, 3, 9, 5 };
int[]B = { 7, 3, 9, 5, 6 };
int size = A.Length;
// Function Call
maximumEqual(A, B, size);
}
}
// This code is contributed by chitranayal
JavaScript
<script>
// JavaScript program of the above approach
// Function that prints maximum
// equal elements
function maximumEqual(a, b, n)
{
// Vector to store the index
// of elements of array b
let store = Array.from({length: 1e5}, (_, i) => 0);
// Storing the positions of
// array B
for (let i = 0; i < n; i++)
{
store[b[i]] = i + 1;
}
// frequency array to keep count
// of elements with similar
// difference in distances
let ans = Array.from({length: 1e5}, (_, i) => 0);
// Iterate through all element in arr1[]
for (let i = 0; i < n; i++)
{
// Calculate number of
// shift required to
// make current element
// equal
let d = Math.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]++;
}
let finalans = 0;
// Compute the maximum frequency
// stored
for (let i = 0; i < 1e5; i++)
finalans = Math.max(finalans,
ans[i]);
// Printing the maximum number
// of equal elements
document.write(finalans + "\n");
}
// Driver Code
// Given two arrays
let A = [ 6, 7, 3, 9, 5 ];
let B = [ 7, 3, 9, 5, 6 ];
let size = A.length;
// Function Call
maximumEqual(A, B, size);
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Javascript 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, 9,
3 min read
Javascript 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 pai
4 min read
Maximise distance by rearranging all duplicates at same distance in given Array Given an array arr[] of N integers. Arrange the array in a way such that the minimum distance among all pairs of same elements is maximum among all possible arrangements. The task is to find this maximum value. Examples: Input: arr[] = {1, 1, 2, 3}Output: 3Explanation: All possible arrangements are:
6 min read
Count elements of same value placed at same indices of two given arrays Given two arrays A[] and B[] of N unique elements, the task is to find the maximum number of matched elements from the two given arrays. Elements of the two arrays are matched if they are of the same value and can be placed at the same index (0-based indexing).(By right shift or left shift of the tw
8 min read
Maximize count of indices with same element by pairing rows from given Matrices Given two 2D binary arrays, a[][] and b[][] both of size M*N, the task is to pair each row in the array a[][] with any row in the array b[][] such that the total score can be maximized and the score for each pair is calculated as the total indexes at which values of both rows are identical. Note: Ea
15+ min read
Maximize product of two closest numbers of other array for every element in given array Given arrays arr1[] of size M and arr2[] of size N having length at least 2, the task is for every element in arr1[], maximize the product of two elements in arr2[] which are closest to the element in arr1[]. The closest elements must be present on distinct indices. Example: Input: arr1 = [5, 10, 17
15 min read