Given two natural numbers N and M, Create a graph using these two natural numbers using relation that a number is connected to its largest factor other than itself. The task is to find the shortest path between these two numbers after creating a graph.

Examples:

Input:_ N = 6, M = 18_

Output:_ 6 <–> 3 <–> 9 <–> 18_

Explanation:

For N = 6, the connection of graph is:

6 — 3 — 1

For N = 18, the connection of graph is:

18 — 9 — 3 — 1

Combining the above two graphs, the shortest path is given by:

6 — 3 — 9 — 18

Input:_ N = 4, M = 8_

Output:_ 4 <–> 8_

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to find the largest factors of each number other than itself and create a graph by connecting these factors and then find the shortest path between them. Below are the steps:

  1. Find the largest common factor of M and store it and set it as M.
  2. Now, until M doesn’t equal to 1 keep repeating the above steps and store the factors generated in an array mfactor[].
  3. Repeat step 1 and step 2 by taking N as the number and store the factors generated in an array nfactor[].
  4. Now, traverse both the arrays mfactor[] and mfactor[] and print the shortest path.

Below is the implementation of the above approach:

  • C++14
  • Python3

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to check the number is

// prime or not

**int** isprm(``**int** n)

{

// Base Cases

**if** (n <= 1)

**return** 0;

**if** (n <= 3)

**return** 1;

**if** (n % 2 == 0 || n % 3 == 0)

**return** 0;

// Iterate till [5, sqrt(N)] to

// detect primarility of numbers

**for** (``**int** i = 5; i * i <= n; i = i + 6)

**if** (n % i == 0 || n % (i + 2) == 0)

**return** 0;

**return** 1;

}

// Function to print the shortest path

**void** shortestpath(``**int** m, **int** n)

{

// Use vector to store the factor

// of m and n

vector<``**int**``> mfactor, nfactor;

// Use map to check if largest common

// factor previously present or not

map<``**int**``, **int**``> fre;

// First store m

mfactor.push_back(m);

fre[m] = 1;

**while** (m != 1) {

// Check whether m is prime or not

**if** (isprm(m)) {

mfactor.push_back(1);

fre[1] = 1;

m = 1;

}

// Largest common factor of m

**else** {

**for** (``**int** i = 2;

i <= **sqrt**``(m); i++) {

// If m is divisible by i

**if** (m % i == 0) {

// Store the largest

// common factor

mfactor.push_back(m / i);

fre[m / i] = 1;

m = (m / i);

**break**``;

}

}

}

}

// For number n

nfactor.push_back(n);

**while** (fre[n] != 1) {

// Check whether n is prime

**if** (isprm(n)) {

nfactor.push_back(1);

n = 1;

}

// Largest common factor of n

**else** {

**for** (``**int** i = 2;

i <= **sqrt**``(n); i++) {

**if** (n % i == 0) {

// Store the largest

// common factor

nfactor.push_back(n / i);

n = (n / i);

**break**``;

}

}

}

}

// Print the path

// Print factors from m

**for** (``**int** i = 0;

i < mfactor.size(); i++) {

// To avoid duplicate printing

// of same element

**if** (mfactor[i] == n)

**break**``;

cout << mfactor[i]

<< " <--> "``;

}

// Print the factors from n

**for** (``**int** i = nfactor.size() - 1;

i >= 0; i--) {

**if** (i == 0)

cout << nfactor[i];

**else**

cout << nfactor[i]

<< " <--> "``;

}

}

// Driver Code

**int** main()

{

// Given N and M

**int** m = 18, n = 19;

// Function Call

shortestpath(m, n);

**return** 0;

}

Output:

18 <--> 9 <--> 3 <--> 1 <--> 19

Time Complexity:_ O(log (max(M, N))_

Auxiliary Space:_ O(N)_

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.

#arrays #graph #greedy #mathematical #algorithms-graph shortest paths quiz #divisors #numbers #prime number #prime-factor

Create a Graph by connecting divisors from N to M and find shortest path
1.35 GEEK