# Minimum Possible sum of digits in a positive multiple of N

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

## Recommended Posts:

- Print first k digits of 1/n where n is a positive integer
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find the smallest positive number which can not be represented by given digits
- First N terms whose sum of digits is a multiple of 10
- Smallest multiple of 3 which consists of three given non-zero digits
- Smallest multiple of N formed using the given set of digits
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Minimum number of changes such that elements are first Negative and then Positive
- Minimum positive integer divisible by C and is not in range [A, B]
- Minimum positive integer to divide a number such that the result is an odd
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Minimum operations to make GCD of array a multiple of k
- Minimum number with digits as 4 and 7 only and given sum
- Minimum number to be added to all digits of X to make X > Y

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.