Given two integers N and M, the task is to find out minimum number of operations required to convert N to M. Each operation involves adding one of the prime factors of the current value of N. If it is posible to obtain M, print the number of operations. Otherwise, print -1.
Input: N = 6, M = 10
Prime factors of 6 are [2, 3].
Adding 2 to N, we obtain 8.
The prime factor of 8 is .
Adding 2 to N, we obtain 10, which is the desired result.
Hence, total steps = 2
Input: N = 2, M = 3
There is no way to convert N = 2 to M = 3.
- Store and precompute all prime numbers using Sieve.
- Now, if N is already equal to M, print 0 as no addition operation is required.
- Visualize this problem as a graph problem to perform BFS. At each level store the reachable numbers from the values of N in the previous level by adding prime factors.
- Now, start by inserting (N, 0), where N denotes the value and 0 denotes the number of operations to reach that value, in the queue initially.
- At each level of the queue, traverse all elements one by one by extracting the element at the front() and perform the following:
- Store q.front().first() in newNum and q.front().second() in distance, where newNum is the current value and distance is the number of operations required to reach this value.
- Store all prime factors of newNum in a set.
- If newNum is equal to M, then print distance, as it is the minimum operations required.
- If newNum is greater than M, then break.
- Otherwise, newNum is less than M. So, update newNum by adding its prime factors i one by one and store (newNum + i, distance + 1) in the queue and repeat the above steps for the next level.
- If the search continues to a level where the queue becomes empty, it means that M cannot be obtained from N. Print -1.
Below is the implementation of the above approach:
Time Complexity: O(N* log(N))
Auxiliary Space: O(N)
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Program to make Indian Flag in Python | Step by Step
- Number of steps to convert to prime factors
- Find number of factors of N when location of its two factors whose product is N is given
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Number of steps to sort the array by changing order of three elements in each step
- Count numbers from range whose prime factors are only 2 and 3
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Sort an array according to the increasing count of distinct Prime Factors
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count common prime factors of two numbers
- Count of nodes in a Binary Tree whose child is its prime factors
- Count of Nodes whose both immediate children are its prime factors
- Find the row whose product has maximum count of prime factors
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.