Difference between maximum and minimum of a set of anagrams from an array | Python3 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 ; Given array ; Size of the array
import math NEW_LINE from collections import defaultdict NEW_LINE def hashFunction ( N ) : NEW_LINE INDENT prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 ] NEW_LINE value = 1 NEW_LINE while ( N != 0 ) : NEW_LINE INDENT r = N % 10 NEW_LINE value = value * prime [ r ] NEW_LINE N = N // 10 NEW_LINE DEDENT return value NEW_LINE DEDENT def findDiff ( arr , n ) : NEW_LINE INDENT m = defaultdict ( lambda : [ ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT h = hashFunction ( arr [ i ] ) NEW_LINE m [ h ] . append ( arr [ i ] ) NEW_LINE DEDENT i = 0 NEW_LINE while ( i != len ( m ) ) : NEW_LINE INDENT if ( len ( m [ i ] ) > 1 ) : NEW_LINE INDENT minn = min ( m [ i ] ) NEW_LINE maxx = max ( m [ i ] ) NEW_LINE print ( maxx - minn ) NEW_LINE break NEW_LINE DEDENT elif ( i == ( len ( m ) - 1 ) ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT arr = [ 121 , 312 , 234 , 211 , 112 , 102 ] NEW_LINE N = len ( arr ) NEW_LINE findDiff ( arr , N ) NEW_LINE