Given two integers N and M, the task is to calculate the minimum number of moves to change N to M, where In one move it is allowed to add any divisor of the current value of N to N itself except 1. Print “-1” if it is not possible.
Example:
Input: N = 4, M = 24
Output: 5
Explanation: The given value of N can be converted into M using the following steps: (4)+2 -> (6)+2 -> (8)+4 -> (12)+6 -> (18)+6 -> 24. Hence, the count of moves is 5 which is the minimum possible.
Input: N = 4, M = 576
Output: 14
BFS Approach: The given problem has already been discussed in Set 1 of this article which using the Breadth First Traversal to solve the given problem.
Approach: This article focused on a different approach based on Dynamic Programming. Below are the steps to follow:
- Create a 1-D array dp[], where dp[i] stores the minimum number of operations to reach i from N. Initially, dp[N+1… M] = {INT_MAX} and dp[N] = 0.
- Iterate through the range [N, M] using a variable i and for each i, iterate through all the factors of the given number i. For a factor X, the DP relation can be define as follows:
dp[i + X] = min( dp[i], dp[i + X])
- The value stored at dp[M] is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperationCnt( int N, int M)
{
int dp[M + 1];
for ( int i = N + 1; i <= M; i++) {
dp[i] = INT_MAX;
}
dp[N] = 0;
for ( int i = N; i <= M; i++) {
if (dp[i] == INT_MAX) {
continue ;
}
for ( int j = 2; j * j <= i; j++) {
if (i % j == 0) {
if (i + j <= M) {
dp[i + j] = min(dp[i + j], dp[i] + 1);
}
if (i + i / j <= M) {
dp[i + i / j]
= min(dp[i + i / j], dp[i] + 1);
}
}
}
}
return (dp[M] == INT_MAX) ? -1 : dp[M];
}
int main()
{
int N = 4;
int M = 576;
cout << minOperationCnt(N, M);
return 0;
}
|
Java
class GFG {
public static int minOperationCnt( int N, int M) {
int [] dp = new int [M + 1 ];
for ( int i = N + 1 ; i <= M; i++) {
dp[i] = Integer.MAX_VALUE;
}
dp[N] = 0 ;
for ( int i = N; i <= M; i++) {
if (dp[i] == Integer.MAX_VALUE) {
continue ;
}
for ( int j = 2 ; j * j <= i; j++) {
if (i % j == 0 ) {
if (i + j <= M) {
dp[i + j] = Math.min(dp[i + j], dp[i] + 1 );
}
if (i + i / j <= M) {
dp[i + i / j] = Math.min(dp[i + i / j], dp[i] + 1 );
}
}
}
}
return (dp[M] == Integer.MAX_VALUE) ? - 1 : dp[M];
}
public static void main(String args[]) {
int N = 4 ;
int M = 576 ;
System.out.println(minOperationCnt(N, M));
}
}
|
Python3
import math
INT_MAX = 2147483647
def minOperationCnt(N, M):
dp = [ 0 for _ in range (M + 1 )]
for i in range (N + 1 , M + 1 ):
dp[i] = INT_MAX
dp[N] = 0
for i in range (N, M + 1 ):
if (dp[i] = = INT_MAX):
continue
for j in range ( 2 , int (math.sqrt(i)) + 1 ):
if (i % j = = 0 ):
if (i + j < = M):
dp[i + j] = min (dp[i + j], dp[i] + 1 )
if (i + i / / j < = M):
dp[i + i / / j] = min (dp[i + i / / j], dp[i] + 1 )
if dp[M] = = INT_MAX:
return - 1
else :
return dp[M]
if __name__ = = "__main__" :
N = 4
M = 576
print (minOperationCnt(N, M))
|
C#
using System;
class GFG
{
public static int minOperationCnt( int N, int M)
{
int [] dp = new int [M + 1];
for ( int i = N + 1; i <= M; i++)
{
dp[i] = int .MaxValue;
}
dp[N] = 0;
for ( int i = N; i <= M; i++)
{
if (dp[i] == int .MaxValue)
{
continue ;
}
for ( int j = 2; j * j <= i; j++)
{
if (i % j == 0)
{
if (i + j <= M)
{
dp[i + j] = Math.Min(dp[i + j], dp[i] + 1);
}
if (i + i / j <= M)
{
dp[i + i / j] = Math.Min(dp[i + i / j], dp[i] + 1);
}
}
}
}
return (dp[M] == int .MaxValue) ? -1 : dp[M];
}
public static void Main()
{
int N = 4;
int M = 576;
Console.Write(minOperationCnt(N, M));
}
}
|
Javascript
<script>
function minOperationCnt(N, M) {
let dp = new Array(M + 1)
for (let i = N + 1; i <= M; i++) {
dp[i] = Number.MAX_VALUE;
}
dp[N] = 0;
for (let i = N; i <= M; i++) {
if (dp[i] == Number.MAX_VALUE) {
continue ;
}
for (let j = 2; j * j <= i; j++) {
if (i % j == 0) {
if (i + j <= M) {
dp[i + j] = Math.min(dp[i + j], dp[i] + 1);
}
if (i + i / j <= M) {
dp[i + i / j]
= Math.min(dp[i + i / j], dp[i] + 1);
}
}
}
}
return (dp[M] == Number.MAX_VALUE) ? -1 : dp[M];
}
let N = 4;
let M = 576;
document.write(minOperationCnt(N, M));
</script>
|
Time Complexity: O((M – N)*√(M – N))
Auxiliary Space: O(M)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Nov, 2022
Like Article
Save Article