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)
- Number of steps to convert to prime factors
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count common prime factors of two numbers
- Count of Nodes whose both immediate children are its prime factors
- Count numbers from range whose prime factors are only 2 and 3
- 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
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Sort an array according to the increasing count of distinct Prime Factors
- Count of nodes in a Binary Tree whose child is its prime factors
- 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
- Prime factors of a big number
- Sum of Factors of a Number using Prime Factorization
- Common prime factors of two numbers
- Distinct Prime Factors of an Array
- Prime factors of LCM of array elements
- Print all prime factors and their powers
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.