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++
#include <bits/stdc++.h>
using namespace std;
void maximumEqual( int a[], int b[],
int n)
{
vector< int > store(1e5);
for ( int i = 0; i < n; i++) {
store[b[i]] = i + 1;
}
vector< int > ans(1e5);
for ( int i = 0; i < n; i++) {
int d = abs (store[a[i]]
- (i + 1));
if (store[a[i]] < i + 1) {
d = n - d;
}
ans[d]++;
}
int finalans = 0;
for ( int i = 0; i < 1e5; i++)
finalans = max(finalans,
ans[i]);
cout << finalans << "\n" ;
}
int main()
{
int A[] = { 6, 7, 3, 9, 5 };
int B[] = { 7, 3, 9, 5, 6 };
int size = sizeof (A) / sizeof (A[0]);
maximumEqual(A, B, size);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maximumEqual( int a[],
int b[], int n)
{
int store[] = new int [( int ) 1e5];
for ( int i = 0 ; i < n; i++)
{
store[b[i]] = i + 1 ;
}
int ans[] = new int [( int ) 1e5];
for ( int i = 0 ; i < n; i++)
{
int d = Math.abs(store[a[i]] - (i + 1 ));
if (store[a[i]] < i + 1 )
{
d = n - d;
}
ans[d]++;
}
int finalans = 0 ;
for ( int i = 0 ; i < 1e5; i++)
finalans = Math.max(finalans,
ans[i]);
System.out.print(finalans + "\n" );
}
public static void main(String[] args)
{
int A[] = { 6 , 7 , 3 , 9 , 5 };
int B[] = { 7 , 3 , 9 , 5 , 6 };
int size = A.length;
maximumEqual(A, B, size);
}
}
|
Python3
def maximumEqual(a, b, n):
store = [ 0 ] * 10 * * 5
for i in range (n):
store[b[i]] = i + 1
ans = [ 0 ] * 10 * * 5
for i in range (n):
d = abs (store[a[i]] - (i + 1 ))
if (store[a[i]] < i + 1 ):
d = n - d
ans[d] + = 1
finalans = 0
for i in range ( 10 * * 5 ):
finalans = max (finalans, ans[i])
print (finalans)
if __name__ = = '__main__' :
A = [ 6 , 7 , 3 , 9 , 5 ]
B = [ 7 , 3 , 9 , 5 , 6 ]
size = len (A)
maximumEqual(A, B, size)
|
C#
using System;
class GFG{
static void maximumEqual( int [] a,
int [] b, int n)
{
int [] store = new int [( int ) 1e5];
for ( int i = 0; i < n; i++)
{
store[b[i]] = i + 1;
}
int [] ans = new int [( int ) 1e5];
for ( int i = 0; i < n; i++)
{
int d = Math.Abs(store[a[i]] - (i + 1));
if (store[a[i]] < i + 1)
{
d = n - d;
}
ans[d]++;
}
int finalans = 0;
for ( int i = 0; i < 1e5; i++)
finalans = Math.Max(finalans, ans[i]);
Console.Write(finalans + "\n" );
}
public static void Main()
{
int []A = { 6, 7, 3, 9, 5 };
int []B = { 7, 3, 9, 5, 6 };
int size = A.Length;
maximumEqual(A, B, size);
}
}
|
Javascript
<script>
function maximumEqual(a, b, n)
{
let store = Array.from({length: 1e5}, (_, i) => 0);
for (let i = 0; i < n; i++)
{
store[b[i]] = i + 1;
}
let ans = Array.from({length: 1e5}, (_, i) => 0);
for (let i = 0; i < n; i++)
{
let d = Math.abs(store[a[i]] - (i + 1));
if (store[a[i]] < i + 1)
{
d = n - d;
}
ans[d]++;
}
let finalans = 0;
for (let i = 0; i < 1e5; i++)
finalans = Math.max(finalans,
ans[i]);
document.write(finalans + "\n" );
}
let A = [ 6, 7, 3, 9, 5 ];
let B = [ 7, 3, 9, 5, 6 ];
let size = A.length;
maximumEqual(A, B, size);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)