Given two integers N and M and the task is to convert N to M with the following operations:
- Multiply N by 2 i.e. N = N * 2.
- Subtract 1 from N i.e. N = N – 1.
Examples:
Input: N = 4, M = 6
Output: 2
Perform operation 2: N = N – 1 = 4 – 1 = 3
Perform operation 1: N = N * 2 = 3 * 2 = 6
Input: N = 10, M = 1
Output: 9
Approach: Create an array dp[] of size MAX = 105 + 5 to store the answer in order to prevent the same computation again and again and initialize all the array elements with -1.
- If N ? 0 or N ? MAX means it can not be converted to M so return MAX.
- If N = M then return 0 as N got converted to M.
- Else find the value at dp[N] if it is not -1, it means it has been calculated earlier so return dp[N].
- If it is -1 then will call the recursive function as 2 * N and N – 1 and return the minimum because if N is odd then it can be reached only by performing N – 1 operation and if N is even then 2 * N operations have to be performed so check both the possibilities and return the minimum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m;
int dp[N];
int minOperations( int k)
{
if (k <= 0 || k >= 2e4) {
return 1e9;
}
if (k == m) {
return 0;
}
int & ans = dp[k];
if (ans != -1) {
return ans;
}
ans = 1e9;
ans = 1 + min(minOperations(2 * k),
minOperations(k - 1));
return ans;
}
int main()
{
n = 4, m = 6;
memset (dp, -1, sizeof (dp));
cout << minOperations(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static final int N = 10000 ;
static int n, m;
static int [] dp = new int [N];
static int minOperations( int k)
{
if (k <= 0 || k >= 10000 )
return 1000000000 ;
if (k == m)
return 0 ;
dp[k] = dp[k];
if (dp[k] != - 1 )
return dp[k];
dp[k] = 1000000000 ;
dp[k] = 1 + Math.min(minOperations( 2 * k),
minOperations(k - 1 ));
return dp[k];
}
public static void main(String[] args)
{
n = 4 ;
m = 6 ;
Arrays.fill(dp, - 1 );
System.out.println(minOperations(n));
}
}
|
Python3
N = 1000
dp = [ - 1 ] * N
def minOperations(k):
if (k < = 0 or k > = 1000 ):
return 1e9
if (k = = m):
return 0
dp[k] = dp[k]
if (dp[k] ! = - 1 ):
return dp[k]
dp[k] = 1e9
dp[k] = 1 + min (minOperations( 2 * k),
minOperations(k - 1 ))
return dp[k]
if __name__ = = '__main__' :
n = 4
m = 6
print (minOperations(n))
|
C#
using System;
using System.Linq;
class GFG
{
static int N = 10000;
static int n, m;
static int [] dp = Enumerable.Repeat(-1, N).ToArray();
static int minOperations( int k)
{
if (k <= 0 || k >= 10000)
return 1000000000;
if (k == m)
return 0;
dp[k] = dp[k];
if (dp[k] != -1)
return dp[k];
dp[k] = 1000000000;
dp[k] = 1 + Math.Min(minOperations(2 * k),
minOperations(k - 1));
return dp[k];
}
public static void Main(String[] args)
{
n = 4;
m = 6;
Console.Write(minOperations(n));
}
}
|
Javascript
<script>
let N = 10000;
let n, m;
let dp = new Array(N);
function minOperations(k)
{
if (k <= 0 || k >= 10000)
return 1000000000;
if (k == m)
return 0;
dp[k] = dp[k];
if (dp[k] != -1)
return dp[k];
dp[k] = 1000000000;
dp[k] = 1 + Math.min(minOperations(2 * k),
minOperations(k - 1));
return dp[k];
}
n = 4;
m = 6;
for (let i = 0; i < dp.length; i++)
{
dp[i] = -1;
}
document.write(minOperations(n));
</script>
|