Smallest multiple of N formed using the given set of digits | Function to return the required number ; Initialize both vectors with their initial values ; Sort the vector of digits ; Initialize the queue ; If modulus value is not present in the queue ; Compute digits modulus given number and update the queue and vectors ; While queue is not empty ; Remove the first element of the queue ; Compute new modulus values by using old queue values and each digit of the set ; If value is not present in the queue ; If required condition can 't be satisfied  ; Initialize new vector ; Constructing the solution by backtracking ; Reverse the vector ; Return the required number ; Driver code
def findSmallestNumber ( arr , n ) : NEW_LINE INDENT dp = [ float ( ' inf ' ) ] * n NEW_LINE result = [ ( - 1 , 0 ) ] * n NEW_LINE arr . sort ( ) NEW_LINE q = [ ] NEW_LINE for i in arr : NEW_LINE INDENT if i != 0 : NEW_LINE INDENT if dp [ i % n ] > 10 ** 9 : NEW_LINE INDENT q . append ( i % n ) NEW_LINE dp [ i % n ] = 1 NEW_LINE result [ i % n ] = - 1 , i NEW_LINE DEDENT DEDENT DEDENT while len ( q ) > 0 : NEW_LINE INDENT u = q . pop ( 0 ) NEW_LINE for i in arr : NEW_LINE INDENT v = ( u * 10 + i ) % n NEW_LINE if dp [ u ] + 1 < dp [ v ] : NEW_LINE INDENT dp [ v ] = dp [ u ] + 1 NEW_LINE q . append ( v ) NEW_LINE result [ v ] = u , i NEW_LINE DEDENT DEDENT DEDENT if dp [ 0 ] > 10 ** 9 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = [ ] NEW_LINE u = 0 NEW_LINE while u != - 1 : NEW_LINE INDENT ans . append ( result [ u ] [ 1 ] ) NEW_LINE u = result [ u ] [ 0 ] NEW_LINE DEDENT ans = ans [ : : - 1 ] NEW_LINE for i in ans : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 2 , 3 ] NEW_LINE n = 12 NEW_LINE print ( findSmallestNumber ( arr , n ) ) NEW_LINE DEDENT