Given a number N, find the minimum possible sum of digits that can be obtained from a positive Multiple of N.

**Constraints **: 1<=N<=10^5.

**Examples:**

Input :N = 6Output :3 Explanation: 6*2 = 12, sum of digits is 1+2 = 3.Input :N = 20Output :1 20*5 = 100, sum of digits is 1+0+0=1

**Approach **: The problem is totally based on observation. For N = 6, the answer is 12.

So we can write 12 as : 1-(*10)–>10–(+1)–>11–(+1)–>12. What the sequence describes is we can write any number >=1, as the sum’s of +1 and product of *10’s, starting with 1.

Another observation is, when we add +1 to the digit, the digit sum increases by +1, unless the last digit is 9.

The second observation is, when we multiply the digit by 10, sum of digits remains constant.

Now, since we want a multiple of N, so any multiple of N % N will be 0.

So, taking the above analysis, we can build a graph of K vertices, for any vertex X, the weight of node from X to X+1 will be one, and weight of node from X to (X*10)%N will be 0. We took modulo N since the vertex lies within 0 to N-1.

The answer will be the shortest distance from 1 to 0, because of (Any multiple of N) mod N = 0.

We can use Dijkstra’s theorem to find the shortest distance between 1 and 0. Let it be d. Answer will be 1+d, as we have also to consider edge 0 to 1.

The time complexity of this solution will be O(E+VLogV) = O(N+NLogN) = O(NlogN) as we will travel at most N vertices and Edges.

One more thing to note is, since, after xyz..9, the next digit will be abc..0, but it will not affect our answer.

**Proof**: lets take example of N = 11, answer is 2. It is from 11*1 = 11 and 11*10 = 110.

Since 110 is a 10’s multiple of N and also an answer, either we have already hit the answer because if 110 is some 10’s multiple of N, then there must exist a number less than this with no leading 0’s in our answer, that is 11.

So either we will surpass 10’s multiple of N, if it is not an answer, or we will have already hit the answer before if some 10’s multiple of N is in answer.

Below is the implementation of the above approach:

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `Maxx = 100005; ` `int` `N; ` `vector<pair<` `int` `, ` `int` `> > Graph[Maxx]; ` ` ` `/// Dijkartas algorithm to find the shortest distance ` `void` `Dijkartas(` `int` `source) ` `{ ` ` ` `priority_queue<pair<` `int` `, ` `int` `>, vector<pair<` `int` `, ` `int` `> >, ` ` ` `greater<pair<` `int` `, ` `int` `> > > PQ; ` ` ` ` ` `// Initialize all distances to be infinity ` ` ` `vector<` `int` `> Distance(N + 2, 1e9); ` ` ` ` ` `// Push source in Priority Queue ` ` ` `PQ.push(make_pair(0, source)); ` ` ` `int` `src = source; ` ` ` `Distance[src] = 0; ` ` ` `while` `(!PQ.empty()) { ` ` ` `int` `current = PQ.top().second; ` ` ` `PQ.pop(); ` ` ` `for` `(` `auto` `& neighbours : Graph[current]) { ` ` ` `int` `v = neighbours.first; ` ` ` `int` `weight = neighbours.second; ` ` ` `if` `(Distance[v] > Distance[current] + weight) { ` ` ` `Distance[v] = Distance[current] + weight; ` ` ` `PQ.push(make_pair(Distance[v], v)); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `cout << ` `"Minimum possible sum of digits is "` `<< ` ` ` `1 + Distance[0] << endl; ` ` ` `return` `; ` `} ` ` ` `// Function to calculate the minimum possible sum of digits ` `void` `minSumDigits(` `int` `N) ` `{ ` ` ` `// Build a graph of N vertices with edge weight 1 ` ` ` `for` `(` `int` `i = 1; i <= N; ++i) { ` ` ` `int` `From = (i) % N; ` ` ` `int` `To = (i + 1) % N; ` ` ` `int` `Wt = 1; ` ` ` `Graph[From].push_back(make_pair(To, Wt)); ` ` ` `} ` ` ` ` ` `// In the same graph add weights 0 to 10's multiple of node X ` ` ` `for` `(` `int` `i = 1; i <= N; ++i) { ` ` ` `int` `From = (i) % N; ` ` ` `int` `To = (10 * i) % N; ` ` ` `int` `Wt = 0; ` ` ` `Graph[From].push_back(make_pair(To, Wt)); ` ` ` `} ` ` ` ` ` `// Run dijartas to find the shortest distance from 1 to 0 ` ` ` `Dijkartas(1); ` ` ` `return` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `N = 19; ` ` ` ` ` `minSumDigits(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Minimum possible sum of digits is 2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum digits to be removed to make either all digits or alternating digits same
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Print all possible ways to write N as sum of two or more positive integers
- Probability that an arbitrary positive divisor of 10^X is an integral multiple of 10^Y
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- First N terms whose sum of digits is a multiple of 10
- Count positive integers with 0 as a digit and maximum 'd' digits
- Print first k digits of 1/n where n is a positive integer
- Find the smallest positive number which can not be represented by given digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Count of digits after concatenation of first N positive integers
- Check if a triangle of positive area is possible with the given angles
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Minimum LCM and GCD possible among all possible sub-arrays
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits

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.