Difference between maximum and minimum of a set of anagrams from an array | C # 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
using System ; using System . Collections . Generic ; using System . Linq ; 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 ) { Dictionary < int , List < int > > m = new Dictionary < int , List < int > > ( ) ; int h , min , max ; for ( int i = 0 ; i < n ; i ++ ) { h = hashFunction ( arr [ i ] ) ; if ( ! m . ContainsKey ( h ) ) { m [ h ] = new List < int > ( ) ; } m [ h ] . Add ( arr [ i ] ) ; } foreach ( KeyValuePair < int , List < int > > i in m ) { if ( i . Value . Count > 1 ) { min = Int32 . MaxValue ; max = Int32 . MinValue ; for ( int j = 0 ; j < i . Value . Count ; j ++ ) { if ( m [ i . Key ] [ j ] < min ) { min = m [ i . Key ] [ j ] ; } if ( m [ i . Key ] [ j ] > max ) { max = m [ i . Key ] [ j ] ; } } Console . Write ( max - min ) ; break ; } else if ( m [ i . Key ] . Equals ( m . Last ( ) . Value ) ) Console . Write ( - 1 ) ; } } static void Main ( ) { int [ ] arr = { 121 , 312 , 234 , 211 , 112 , 102 } ; int N = arr . Length ; findDiff ( arr , N ) ; } }