Open In App
Related Articles

Javascript Program to Maximize count of corresponding same elements in given Arrays by Rotation

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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:
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:
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: 
 

  1. Store the position of all the elements of the array arr2[] in an array(say store[]).
  2. 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.
  3. 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: 
 

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 + "
");
}
 
// 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>

                    

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!
 



Last Updated : 04 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads