Rearrangement of a number which is also divisible by it | Perform hashing for given n ; perform hashing ; check whether any arrangement exists ; Create a hash for given number n The hash is of size 10 and stores count of each digit in n . ; check for all possible multipliers ; check hash table for both . ; Driver Code
def storeDigitCounts ( n , Hash ) : NEW_LINE INDENT while n > 0 : NEW_LINE INDENT Hash [ n % 10 ] += 1 NEW_LINE n //= 10 NEW_LINE DEDENT DEDENT def rearrange ( n ) : NEW_LINE INDENT hash_n = [ 0 ] * 10 NEW_LINE storeDigitCounts ( n , hash_n ) NEW_LINE for mult in range ( 2 , 10 ) : NEW_LINE INDENT curr = n * mult NEW_LINE hash_curr = [ 0 ] * 10 NEW_LINE storeDigitCounts ( curr , hash_curr ) NEW_LINE if hash_n == hash_curr : NEW_LINE INDENT return curr NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10035 NEW_LINE print ( rearrange ( n ) ) NEW_LINE DEDENT