Open In App

# 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 = 6
Output : 3
Explanation: 6*2 = 12, sum of digits is 1+2 = 3.

Input : N = 20
Output : 1
20*5 = 100, sum of digits is 1+0+0=1```

## CPP

 `#include ``using` `namespace` `std;` `const` `int` `Maxx = 100005;``int` `N;``vector > Graph[Maxx];` `/// Dijkartas algorithm to find the shortest distance``void` `Dijkartas(``int` `source)``{``    ``priority_queue, vector >,``                                ``greater > > 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 << 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 dijkartas to find the shortest distance from 1 to 0``    ``Dijkartas(1);``    ``return``;``}` `// Driver Code``int` `main()``{``    ``N = 19;` `    ``minSumDigits(N);` `    ``return` `0;``}`

## Python3

 `# Python3 code to implement the approach``Maxx ``=` `100005``Graph ``=` `[``None` `for` `_ ``in` `range``(Maxx)]``for` `i ``in` `range``(Maxx):``    ``Graph[i] ``=` `[]` `# / Dijkartas algorithm to find the shortest distance``def` `Dijkartas(source):` `    ``PQ ``=` `[]` `    ``# Initialize all distances to be infinity``    ``Distance ``=` `[``1e9` `for` `_ ``in` `range``(N ``+` `2``)]` `    ``# append source in Priority Queue``    ``PQ.append([``0``, source])``    ``src ``=` `source``    ``Distance[src] ``=` `0``    ``while` `(``len``(PQ) !``=` `0``):``        ``current ``=` `PQ.pop(``0``)[``1``]``        ``for` `neighbours ``in` `Graph[current]:``            ``v ``=` `neighbours[``0``]``            ``weight ``=` `neighbours[``1``]``            ``if` `(Distance[v] > Distance[current] ``+` `weight):``                ``Distance[v] ``=` `Distance[current] ``+` `weight``                ``PQ.append([Distance[v], v])` `    ``print``(``"Minimum possible sum of digits is"``, (``1` `+` `Distance[``0``]))``    ``return` `# Function to calculate the minimum possible sum of digits``def` `minSumDigits(N):` `    ``# Build a graph of N vertices with edge weight 1``    ``for` `i ``in` `range``(``1``, N ``+` `1``):``        ``From ``=` `(i) ``%` `N``        ``To ``=` `(i ``+` `1``) ``%` `N``        ``Wt ``=` `1``        ``Graph[From].append([To, Wt])` `    ``# In the same graph add weights 0 to 10's multiple of node X``    ``for` `i ``in` `range``(``1``, N ``+` `1``):``        ``From ``=` `(i) ``%` `N``        ``To ``=` `(``10` `*` `i) ``%` `N``        ``Wt ``=` `0``        ``Graph[From].append([To, Wt])` `    ``# Run dijkartas to find the shortest distance from 1 to 0``    ``Dijkartas(``1``)``    ``return` `# Driver Code``N ``=` `19``minSumDigits(N)` `# This code is contributed by phasing17`

## C#

 `// C# code to implement the approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG {``    ``static` `int` `Maxx = 100005;``    ``static` `int` `N;``    ``static` `List > Graph``        ``= ``new` `List >();` `    ``/// Dijkartas algorithm to find the shortest distance``    ``static` `void` `Dijkartas(``int` `source)``    ``{``        ``List<``int``[]> PQ = ``new` `List<``int``[]>();` `        ``// Initialize all distances to be infinity``        ``int``[] Distance = ``new` `int``[N + 2];``        ``for` `(``int` `i = 0; i <= N + 1; i++)``            ``Distance[i] = 1000000000;` `        ``// Push source in Priority Queue``        ``PQ.Add(``new``[] { 0, source });``        ``int` `src = source;``        ``Distance[src] = 0;``        ``while` `(PQ.Count != 0) {``            ``int` `current = PQ;``            ``PQ.RemoveAt(0);``            ``foreach``(``var` `neighbours ``in` `Graph[current])``            ``{``                ``int` `v = neighbours;``                ``int` `weight = neighbours;``                ``if` `(Distance[v]``                    ``> Distance[current] + weight) {``                    ``Distance[v]``                        ``= Distance[current] + weight;``                    ``PQ.Add(``new``[] { Distance[v], v });``                ``}``            ``}``        ``}` `        ``Console.WriteLine(``            ``"Minimum possible sum of digits is "``            ``+ (1 + Distance));``        ``return``;``    ``}` `    ``// Function to calculate the minimum possible sum of``    ``// digits``    ``static` `void` `minSumDigits(``int` `N)``    ``{``        ``// Build a graph of N vertices with edge weight 1``        ``for` `(``var` `i = 1; i <= N; ++i) {``            ``int` `From = (i) % N;``            ``int` `To = (i + 1) % N;``            ``int` `Wt = 1;``            ``List<``int``[]> l1 = Graph[From];``            ``l1.Add(``new``[] { To, Wt });``            ``Graph[From] = l1;``        ``}` `        ``// 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;``            ``List<``int``[]> l1 = Graph[From];``            ``l1.Add(``new``[] { To, Wt });``            ``Graph[From] = l1;``        ``}` `        ``// Run dijkartas to find the shortest distance from 1``        ``// to 0``        ``Dijkartas(1);``        ``return``;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``N = 19;``        ``for` `(``int` `i = 0; i < Maxx; i++)``            ``Graph.Add(``new` `List<``int``[]>());``        ``minSumDigits(N);``    ``}``}` `// This code is contributed by phasing17`

## Javascript

 `// JS code to implement the approach` `let Maxx = 100005;``let N;``let Graph = ``new` `Array(Maxx);``for` `(``var` `i = 0; i < Maxx; i++)``    ``Graph[i] = ``new` `Array();` `/// Dijkartas algorithm to find the shortest distance``function` `Dijkartas(source)``{``    ``let PQ = [];` `    ``// Initialize all distances to be infinity``    ``let Distance = ``new` `Array(N + 2).fill(1e9);` `    ``// Push source in Priority Queue``    ``PQ.push([0, source]);``    ``let src = source;``    ``Distance[src] = 0;``    ``while` `(PQ.length != 0) {``        ``let current = PQ.shift();``        ``for` `(let neighbours of Graph[current]) {``            ``let v = neighbours;``            ``let weight = neighbours;``            ``if` `(Distance[v] > Distance[current] + weight) {``                ``Distance[v] = Distance[current] + weight;``                ``PQ.push([Distance[v], v]);``            ``}``        ``}``    ``}` `    ``console.log(``"Minimum possible sum of digits is"``, (1 + Distance));``    ``return``;``}` `// Function to calculate the minimum possible sum of digits``function` `minSumDigits(N)``{``    ``// Build a graph of N vertices with edge weight 1``    ``for` `(``var` `i = 1; i <= N; ++i) {``        ``let From = (i) % N;``        ``let To = (i + 1) % N;``        ``let Wt = 1;``        ``Graph[From].push([To, Wt]);``    ``}` `    ``// In the same graph add weights 0 to 10's multiple of node X``    ``for` `(``var` `i = 1; i <= N; ++i) {``        ``let From = (i) % N;``        ``let To = (10 * i) % N;``        ``let Wt = 0;``        ``Graph[From].push([To, Wt]);``    ``}` `    ``// Run dijkartas to find the shortest distance from 1 to 0``    ``Dijkartas(1);``    ``return``;``}` `// Driver Code``N = 19;``minSumDigits(N);`  `// This code is contributed by phasing17`

## Java

 `// Java code to implement the approach` `import` `java.util.*;` `class` `GFG {``    ``static` `int` `Maxx = ``100005``;``    ``static` `int` `N;``    ``static` `ArrayList > > Graph``        ``= ``new` `ArrayList > >();` `    ``/// Dijkartas algorithm to find the shortest distance``    ``static` `void` `Dijkartas(``int` `source)``    ``{``        ``ArrayList > PQ``            ``= ``new` `ArrayList >();` `        ``// Initialize all distances to be infinity``        ``ArrayList Distance``            ``= ``new` `ArrayList();``        ``for` `(``int` `i = ``0``; i <= N + ``1``; i++)``            ``Distance.add(``1000000000``);` `        ``// Push source in Priority Queue``        ``ArrayList l1 = ``new` `ArrayList();``        ``l1.add(``0``);``        ``l1.add(source);``        ``PQ.add(l1);``        ``int` `src = source;``        ``Distance.set(src, ``0``);``        ``while` `(PQ.size() != ``0``) {``            ``int` `current = PQ.get(``0``).get(``1``);``            ``PQ.remove(``0``);``            ``for` `(ArrayList neighbours :``                 ``Graph.get(current)) {``                ``int` `v = neighbours.get(``0``);``                ``int` `weight = neighbours.get(``1``);``                ``if` `(Distance.get(v)``                    ``> Distance.get(current) + weight) {``                    ``Distance.set(v, Distance.get(current)``                                        ``+ weight);``                    ``ArrayList l2``                        ``= ``new` `ArrayList();``                    ``l2.add(Distance.get(v));``                    ``l2.add(v);``                    ``PQ.add(l2);``                ``}``            ``}``        ``}` `        ``System.out.println(``            ``"Minimum possible sum of digits is "``            ``+ (``1` `+ Distance.get(``0``)));``        ``return``;``    ``}` `    ``// Function to calculate the minimum possible sum of``    ``// digits``    ``static` `void` `minSumDigits(``int` `N)``    ``{``        ``// Build a graph of N vertices with edge weight 1``        ``for` `(var i = ``1``; i <= N; ++i) {``            ``int` `From = (i) % N;``            ``int` `To = (i + ``1``) % N;``            ``int` `Wt = ``1``;``            ``ArrayList > l1``                ``= Graph.get(From);``            ``ArrayList l2``                ``= ``new` `ArrayList();``            ``l2.add(To);``            ``l2.add(Wt);``            ``l1.add(l2);``            ``Graph.set(From, l1);``        ``}` `        ``// 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``;``            ``ArrayList > l1``                ``= Graph.get(From);``            ``ArrayList l2``                ``= ``new` `ArrayList();``            ``l2.add(To);``            ``l2.add(Wt);``            ``l1.add(l2);``            ``Graph.set(From, l1);``        ``}` `        ``// Run dijkartas to find the shortest distance from 1``        ``// to 0``        ``Dijkartas(``1``);``        ``return``;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``N = ``19``;``        ``for` `(``int` `i = ``0``; i < Maxx; i++)``            ``Graph.add(``new` `ArrayList >());``        ``minSumDigits(N);``    ``}``}` `// This code is contributed by phasing17`

Output:

`Minimum possible sum of digits is 2`

Time complexity: O(N log N) where N is the number of vertices in the graph.

Space complexity: O(N)