text: Maximize minimum distance between repetitions from any permutation of the given Array | Java program to implement the above approach ; Stores the frequency of array elements ; Find the highest frequency in the array ; Increase count of max frequent element ; If no repetition is present ; Find the maximum distance ; Return the max distance ; Driver Code
code: class GFG { static int findMaxLen ( int a [ ] , int n ) { int freq [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { freq [ a [ i ] ] ++ ; } int maxFreqElement = Integer . MIN_VALUE ; int maxFreqCount = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { if ( freq [ i ] > maxFreqElement ) { maxFreqElement = freq [ i ] ; maxFreqCount = 1 ; } else if ( freq [ i ] == maxFreqElement ) maxFreqCount ++ ; } int ans ; if ( maxFreqElement == 1 ) ans = 0 ; else { ans = ( ( n - maxFreqCount ) / ( maxFreqElement - 1 ) ) ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 2 } ; int n = a . length ; System . out . print ( findMaxLen ( a , n ) ) ; } }