Difference between maximum and minimum of a set of anagrams from an array | 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 ; Given array ; Size of the array
function hashFunction ( N ) { let prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 ] ; let value = 1 , r ; while ( N != 0 ) { r = N % 10 ; value = value * prime [ r ] ; N = parseInt ( N / 10 , 10 ) ; } return value ; } function findDiff ( arr , n ) { let m = new Map ( ) ; let h , min , max ; for ( let i = 0 ; i < n ; i ++ ) { h = hashFunction ( arr [ i ] ) ; if ( ! m . has ( h ) ) { m . set ( h , [ ] ) ; } ( m . get ( h ) ) . push ( arr [ i ] ) ; } m . forEach ( ( values , keys ) => { if ( values . length > 1 ) { min = Number . MAX_VALUE ; max = Number . MIN_VALUE ; for ( let j = 0 ; j < values . length ; j ++ ) { if ( ( m . get ( keys ) ) [ j ] < min ) { min = m . get ( keys ) [ j ] ; } if ( m . get ( keys ) [ j ] > max ) { max = m . get ( keys ) [ j ] ; } } document . write ( max - min ) ; } } ) } let arr = [ 121 , 312 , 234 , 211 , 112 , 102 ] ; let N = arr . length ; findDiff ( arr , N ) ;