Difference between maximum and minimum of a set of anagrams from an array | Java program for the above approach ; Utility function to find the hash value for each element of the given array ; Initialize an array with first 10 prime numbers ; Iterate over digits of N ; Update Hash Value ; Update N ; Function to find the set of anagrams in the array and print the difference between the maximum and minimum of these numbers ; Map to store the hash value and the array elements having that hash value ; Find the hash value for each arr [ i ] by calling hash function ; Iterate over the map ; If size of vector at m [ i ] greater than 1 then it must contain the anagrams ; Find the minimum and maximum element of this anagrams vector ; Display the difference ; If the end of Map is reached , then no anagrams are present ; Driver code ; Given array ; Size of the array
import java . util . * ; class GFG { static int hashFunction ( int N ) { int [ ] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 } ; int value = 1 , r ; while ( N != 0 ) { r = N % 10 ; value = value * prime [ r ] ; N = N / 10 ; } return value ; } static void findDiff ( int [ ] arr , int n ) { HashMap < Integer , Vector < Integer > > m = new HashMap < > ( ) ; int h , min , max ; for ( int i = 0 ; i < n ; i ++ ) { h = hashFunction ( arr [ i ] ) ; if ( ! m . containsKey ( h ) ) { m . put ( h , new Vector < Integer > ( ) ) ; } m . get ( h ) . add ( arr [ i ] ) ; } for ( Map . Entry < Integer , Vector < Integer > > i : m . entrySet ( ) ) { if ( i . getValue ( ) . size ( ) > 1 ) { min = Integer . MAX_VALUE ; max = - ( Integer . MAX_VALUE ) ; for ( int j = 0 ; j < i . getValue ( ) . size ( ) ; j ++ ) { if ( m . get ( i . getKey ( ) ) . get ( j ) < min ) { min = m . get ( i . getKey ( ) ) . get ( j ) ; } if ( m . get ( i . getKey ( ) ) . get ( j ) > max ) { max = m . get ( i . getKey ( ) ) . get ( j ) ; } } System . out . print ( max - min ) ; break ; } else if ( m . get ( i . getKey ( ) ) == m . values ( ) . toArray ( ) [ m . size ( ) - 1 ] ) System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 121 , 312 , 234 , 211 , 112 , 102 } ; int N = arr . length ; findDiff ( arr , N ) ; } }