Maximize count of corresponding same elements in given Arrays by Rotation | Java program of the above approach ; Function that prints maximum equal elements ; Vector to store the index of elements of array b ; Storing the positions of array B ; frequency array to keep count of elements with similar difference in distances ; Iterate through all element in arr1 [ ] ; Calculate number of shift required to make current element equal ; If d is less than 0 ; Store the frequency of current diff ; Compute the maximum frequency stored ; Printing the maximum number of equal elements ; Driver Code ; Given two arrays ; Function Call
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 + "NEW_LINE"); } 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 ) ; } }