Convert a number m to n with minimum operations. The operations allowed are :
- Multiply by 2, i.e., do m = 2 * m
- Subtract 1, i.e., do m = m – 1
Print -1 if it is not possible to convert.
Examples :
Input : m = 3, n = 11 Output : 3 1st operation: *2 = 3*2 = 6 2nd operation: *2 = 6*2 = 12 3rd operation: -1 = 12-1 = 11 Input : m = 15, n = 20 Output : 6 1st operation: -1 '5' times = 15 + (-1*5) = 10 2nd operation: *2 = 10*2 = 20 Input : m = 0, n = 8 Output : -1 Using the given set of operations 'm' cannot be converted to 'n' Input : m = 12, n = 8 Output : 4
The idea is based on below facts.
1) If m is less than 0 and n is greater than 0, then not possible.
2) If m is greater than n, then we can reach n using subtractions only.
3) Else (m is less than n), we must do m*2 operations. Following two cases arise.
……a) If n is odd, we must do a -1 operation to reach it.
……b) If n is even, we must do a *2 operation to reach it.
Algorithm:
int convert(m,n)
if (m == n)
return 0;
// not possible
if (m <= 0 && n > 0)
return -1;
// m is greater than n
if (m > n)
return m-n;
// n is odd
if (n % 2 == 1)
// perform '-1'
return 1 + convert(m, n+1);
// n is even
else
// perform '*2'
return 1 + convert(m, n/2);
Note: The list of operations so generated should be applied in reverse order.
For example:
m = 3, n = 11
convert(3,11)
| --> n is odd: operation '-1'
convert(3,12)
| --> n is even: operation '*2'
convert(3,6)
| --> n is even: operation '*2'
convert(3,3)
| --> m == n
return
Therefore, the sequence of operations is '*2', '*2', '-1'.
C++
// C++ implementation to convert // a number m to n using minimum // number of given operations #include <bits/stdc++.h> using namespace std; // Function to find minimum // number of given operations // to convert m to n int convert(int m, int n) { if (m == n) return 0; // only way is to do // -1 (m - n) times if (m > n) return m - n; // not possible if (m <= 0 && n > 0) return -1; // n is greater and n is odd if (n % 2 == 1) // perform '-1' on m // (or +1 on n) return 1 + convert(m, n + 1); // n is even else // perform '*2' on m // (or n/2 on n) return 1 + convert(m, n / 2); } // Driver code int main() { int m = 3, n = 11; cout << "Minimum number of operations : " << convert(m, n); return 0; } |
Java
// Java implementation to convert // a number m to n using minimum // number of given operations class ConvertNum { // function to find minimum // number of given operations // to convert m to n static int convert(int m, int n) { if (m == n) return 0; // only way is to do // -1 (m - n) times if (m > n) return m - n; // not possible if (m <= 0 && n > 0) return -1; // n is greater and n is odd if (n % 2 == 1) // perform '-1' on m // (or +1 on n) return 1 + convert(m, n + 1); // n is even else // perform '*2' on m // (or n / 2 on n) return 1 + convert(m, n / 2); } // Driver Code public static void main (String[] args) { int m = 3, n = 11; System.out.println("Minimum number of " + "operations : " + convert(m, n)); } } |
Python3
# Python implementation to convert # a number m to n using minimum # number of given operations # Function to find minimum # number of given operations # to convert m to n def conver(m, n): if(m == n): return 0 # only way is to do # -1(m - n): times if(m > n): return m - n # not possible if(m <= 0 and n > 0): return -1 # n is greater and n is odd if(n % 2 == 1): # perform '-1' on m #(or +1 on n): return 1 + conver(m, n + 1) # n is even else: # perform '*2' on m #(or n/2 on n): return 1 + conver(m, n / 2) # Driver code m = 3n = 11print("Minimum number of operations :", conver(m, n)) # This code is contributed by # Sanjit_Prasad |
C#
// C# implementation to convert // a number m to n using minimum // number of given operations using System; class GFG { // function to find minimum // number of given operations // to convert m to n static int convert(int m, int n) { if (m == n) return 0; // only way is to do // -1 (m - n) times if (m > n) return m - n; // not possible if (m <= 0 && n > 0) return -1; // n is greater and n is odd if (n % 2 == 1) // perform '-1' on m // (or +1 on n) return 1 + convert(m, n + 1); // n is even else // perform '*2' on m // (or n / 2 on n) return 1 + convert(m, n / 2); } // Driver code public static void Main () { int m = 3, n = 11; Console.Write("Minimum number of " + "operations : " + convert(m, n)); } } // This code is contributed by nitin mittal |
PHP
<?php // PHP implementation to convert // a number m to n using minimum // number of given operations // Function to find minimum // number of given operations // to convert m to n function convert($m, $n) { if ($m == $n) return 0; // only way is to do // -1 (m - n) times if ($m > $n) return $m - $n; // not possible if ($m <= 0 && $n > 0) return -1; // n is greater and n is odd if ($n % 2 == 1) // perform '-1' on m // (or +1 on n) return 1 + convert($m, $n + 1); // n is even else // perform '*2' on m // (or n/2 on n) return 1 + convert($m, $n / 2); } // Driver code { $m = 3; $n = 11; echo "Minimum number of ". "operations : ", convert($m, $n); return 0; } // This code is contributed // by nitin mittal. ?> |
Output :
Minimum number of operations : 3
References :
http://tech.queryhome.com/112705/convert-number-with-minimum-operations-operations-allowed
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
Recommended Posts:
- Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2
- Minimize cost to convert given two integers to zero using given operations
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Minimum operations required to convert all characters of a String to a given Character
- Minimum prime number operations to convert A to B
- Minimum number operations required to convert n to m | Set-2
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Convert N to M with given operations using dynamic programming
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum Cost to make all array elements equal using given operations
- Minimum number of given operations required to reduce the array to 0 element
- Reduce N to 1 with minimum number of given operations
- Minimum number of operations required to obtain a given Binary String
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
- Maximize the Sum of the given array using given operations
- Minimum operations to make all elements equal using the second array
- Minimum operations of the given type required to make a complete graph
- Make all the array elements odd with minimum operations of given type
- Minimum increment/decrement operations required on Array to satisfy given conditions
- Minimum steps to reduce N to 0 by given operations

