Open In App

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

Last Updated : 28 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
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: 
 

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


Output: 

5

 

Time Complexity: O(N) 
Auxiliary Space: O(N)

Another approach:

  1. Create a hash map called positions to store the positions of the elements in array B.
  2. Iterate through the elements of array B, and store the position of each element in the positions hash map.
  3. Create an array called ans to store the frequency of each difference between the positions of the elements in arrays A and B
  4. Iterate through the elements of array A, and for each element:
    1.Calculate the number of shifts required to make the current element equal to the corresponding element in array B.
    2.If the difference between the positions is less than 0, update the difference to (n – difference).
    3.Store the frequency of the current difference in the ans array.
  5. Return the maximum frequency stored in the ans array.

Python3




def maximumEqual(a, b, n):
    # Hash map to store positions of elements in array B
    positions = {}
 
    # Store positions of elements in array B
    for i, x in enumerate(b):
        positions[x] = i + 1
 
    # Frequency array to keep count of elements with similar difference in positions
    ans = [0] * n
 
    # Iterate through elements of array A
    for i, x in enumerate(a):
        # Calculate number of shifts required to make current element equal
        d = abs(positions[x] - (i + 1))
        # If d is less than 0, update d to (n - d)
        if positions[x] < i + 1:
            d = n - d
        # Store the frequency of current difference
        ans[d] += 1
 
    # Return maximum frequency stored
    return max(ans)
 
  # Given two arrays
A = [ 6, 7, 3, 9, 5 ]
B = [ 7, 3, 9, 5, 6 ]
 
 
size = len(A)
 
    # Function Call
print(maximumEqual(A, B, size))


Output

5

 Time complexity: O(N), since we are iterating through the elements of both arrays once. 
Auxiliary space: O(N), since we are using a hash map and an array to store the positions of the elements in array B and the frequencies of the differences between the positions, respectively. 

Please refer complete article on Maximize count of corresponding same elements in given Arrays by Rotation for more details!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads