# Smallest multiple of N formed using the given set of digits

Given a set of digits **S** and an integer **N**, the task is to find the smallest positive integer if exists which contains only the digits from **S** and is a multiple of **N**. **Note** that the digits from the set can be used multiple times.

**Examples:**

Input:S[] = {5, 2, 3}, N = 12

Output:252

We can observe that 252 is formed using {2, 5} and is a multiple of 12

Input:S[] = {1, 3, 5, 7, 9}, N = 2

Output:-1

Multiple of 2 would always be an even number but from the given set of digits even number can’t be formed.

A **simple approach** is to sort the set of digits and then move from smallest to the largest number formed using the given digits. We check each number whether it satisfies the given condition. Implementing it this way would result in exponential time complexity.

A **better approach** is to use Modular Arithmetic. So, we maintain a queue in which we will store the modulus of numbers formed using set of digits with the given number **N**. Initially in the queue, there would be (single digit number) % N but we can calculate (double digit number) % N by using,

New_mod = (Old_mod * 10 + digit) % N

By using the above expression we can calculate modulus values of multiple digit numbers. This is an application of dynamic programming as we are building our solution from smaller state to larger state. We maintain an another vector to ensure that element to be inserted in queue is already present in the queue or not. It uses hashing to ensure O(1) time complexity. We used another vector of pair which also uses hashing to store the values and its structure is

result[new_mod] = { current_element_of_queue, digit}

This vector would be used to construct the solution:

- Sort the set of digits.
- Initialize two vectors dp and result, with INT_MAX and {-1, 0} respectively.
- Initialize a queue and insert digit % N.
- Do while queue is not empty.
- Remove front value from queue and for each digit in the set, find (formed number) % N using above written expression.
- If we didn’t get 0 as a modulus value before queue is empty then smallest positive number does not exist else trace the result vector from the 0th index until we get -1 at any index.
- Put all these values in another vector and reverse it.
- This is our required solution.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the required number ` `int` `findSmallestNumber(vector<` `int` `>& arr, ` `int` `n) ` `{ ` ` ` ` ` `// Initialize both vectors with their initial values ` ` ` `vector<` `int` `> dp(n, numeric_limits<` `int` `>::max() - 5); ` ` ` `vector<pair<` `int` `, ` `int` `> > result(n, make_pair(-1, 0)); ` ` ` ` ` `// Sort the vector of digits ` ` ` `sort(arr.begin(), arr.end()); ` ` ` ` ` `// Initialize the queue ` ` ` `queue<` `int` `> q; ` ` ` `for` `(` `auto` `i : arr) { ` ` ` `if` `(i != 0) { ` ` ` ` ` `// If modulus value is not present ` ` ` `// in the queue ` ` ` `if` `(dp[i % n] > 1e9) { ` ` ` ` ` `// Compute digits modulus given number and ` ` ` `// update the queue and vectors ` ` ` `q.push(i % n); ` ` ` `dp[i % n] = 1; ` ` ` `result[i % n] = { -1, i }; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// While queue is not empty ` ` ` `while` `(!q.empty()) { ` ` ` ` ` `// Remove the first element of the queue ` ` ` `int` `u = q.front(); ` ` ` `q.pop(); ` ` ` `for` `(` `auto` `i : arr) { ` ` ` ` ` `// Compute new modulus values by using old queue ` ` ` `// values and each digit of the set ` ` ` `int` `v = (u * 10 + i) % n; ` ` ` ` ` `// If value is not present in the queue ` ` ` `if` `(dp[u] + 1 < dp[v]) { ` ` ` `dp[v] = dp[u] + 1; ` ` ` `q.push(v); ` ` ` `result[v] = { u, i }; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// If required condition can't be satisfied ` ` ` `if` `(dp[0] > 1e9) ` ` ` `return` `-1; ` ` ` `else` `{ ` ` ` ` ` `// Initialize new vector ` ` ` `vector<` `int` `> ans; ` ` ` `int` `u = 0; ` ` ` `while` `(u != -1) { ` ` ` ` ` `// Constructing the solution by backtracking ` ` ` `ans.push_back(result[u].second); ` ` ` `u = result[u].first; ` ` ` `} ` ` ` ` ` `// Reverse the vector ` ` ` `reverse(ans.begin(), ans.end()); ` ` ` `for` `(` `auto` `i : ans) { ` ` ` ` ` `// Return the required number ` ` ` `return` `i; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr = { 5, 2, 3 }; ` ` ` `int` `n = 12; ` ` ` ` ` `cout << findSmallestNumber(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

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

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Smallest multiple of 3 which consists of three given non-zero digits
- Smallest multiple of a given number made of digits 0 and 9 only
- Smallest multiple of N with exactly N digits in its Binary number representation
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Smallest square formed with given rectangles
- Smallest integer with digit sum M and multiple of N
- Smallest N digit number which is a multiple of 5
- Length of the smallest number which is divisible by K and formed by using 1's only
- First N terms whose sum of digits is a multiple of 10
- Maximum possible time that can be formed from four digits
- Sum of all numbers that can be formed with permutations of n digits
- Smallest subarray whose sum is multiple of array size
- Minimum Possible sum of digits in a positive multiple of N
- Find the largest number that can be formed with the given digits
- N digit numbers divisible by 5 formed from the M digits
- Check if B can be formed by permuting the binary digits of A
- Minimum sum of two numbers formed from digits of an array in O(n)
- Minimum sum of two numbers formed from digits of an array
- Count numbers formed by given two digit with sum having given digits
- Greatest number less than equal to B that can be formed from the digits of A

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.