# Java 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, 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:

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

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!

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next
{"ft_lang_label":"__label__en","ft_lang_prob":0.7703371,"math_prob":0.99277467,"size":2575,"snap":"2023-40-2023-50","text_gpt3_token_len":792,"char_repetition_ratio":0.13574484,"word_repetition_ratio":0.17460318,"special_character_ratio":0.34563106,"punctuation_ratio":0.18592058,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9981081,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-04T00:25:30Z\",\"WARC-Record-ID\":\"<urn:uuid:f747c858-8f1f-43e7-ad2b-c72e7f5d2c41>\",\"Content-Length\":\"356458\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f3f21ec5-e306-446e-836e-493c319dcec3>\",\"WARC-Concurrent-To\":\"<urn:uuid:a2dcd96f-a854-4f62-b7d7-2b3aae850483>\",\"WARC-IP-Address\":\"108.138.64.101\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/java-program-to-maximize-count-of-corresponding-same-elements-in-given-arrays-by-rotation/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:TWRTAABZLTTEMBHHS3BNPRALGVJ2S3AO\",\"WARC-Block-Digest\":\"sha1:6HUEW252LEMCBRY2SYVUSUMV4BX7ESZI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100518.73_warc_CC-MAIN-20231203225036-20231204015036-00244.warc.gz\"}"}