Convert N to K by multiplying by A or B in minimum steps
Given two numbers N and K, and a pair A and B, the task is to multiply N by A or B as many times to get K in minimum steps. If it is not possible to obtain K from N, return -1;
Examples:
Input: n = 4, k = 5184, a = 2, b = 3
Output: 8
Explanation: 4→12→24→72→144→432→1296→2592→5184Input: n = 5, k = 13, a = 2, b = 3
Output: -1
Approach: The given problem can be solved by using recursion.
Follow the steps to solve this problem:
- Initialize a count variable, Cnt = 0.
- Call a recursive function to solve with a parameter n, k, a, b, Cnt.
- Check, if n == k, return Cnt.
- Else if, n > k, return INT_MAX;
- Call, min(solve(a*n, k, a, b, Cnt + 1), solve(b*n, k, a, b, Cnt + 1)).
- If, the return value is not equal to INT_MAX then print it else print -1.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to find minimum steps int solve(int n, int& k, int& a, int& b, int Cnt) { if (n == k) { return Cnt; } if (n > k) { return INT_MAX; } return min(solve(a * n, k, a, b, Cnt + 1), solve(b * n, k, a, b, Cnt + 1)); } // Driver Code int main() { int n = 4; int k = 5184; int a = 2, b = 3; // Function Call int res = solve(n, k, a, b, 0); if (res != INT_MAX) cout << res << endl; else cout << -1 << endl; return 0; }
Java
// Java code to implement the approach import java.io.*; public class GFG{ public static int MaxValue = 2147483647; // Function to find minimum steps static int solve(int n, int k, int a, int b, int Cnt) { if (n == k) { return Cnt; } if (n > k) { return MaxValue ; } return Math.min(solve(a * n, k, a, b, Cnt + 1), solve(b * n, k, a, b, Cnt + 1)); } static public void main(String args[]){ int n = 4; int k = 5184; int a = 2, b = 3; // Function Call int res = solve(n, k, a, b, 0); if (res != MaxValue) System.out.println(res); else System.out.println(-1); } } // This code is contributed by AnkThon
Python3
# Python code to implement the approach # declare a max number maxi = 9223372036854775807 # Function to find minimum steps def solve(n, k, a, b, Cnt): if (n == k): return Cnt if (n > k): return maxi return min(solve(a * n, k, a, b, Cnt + 1) , solve(b * n, k, a, b, Cnt + 1)) # Driver Code n = 4 k = 5184 a = 2 b = 3 # Function Call res = solve(n, k, a, b, 0) if (res != maxi): print(res) else: print(-1) # this code is contributed by ksam24000
C#
// C# code to implement the approach using System; public class GFG{ public const int MaxValue = 2147483647; // Function to find minimum steps static int solve(int n, int k, int a, int b, int Cnt) { if (n == k) { return Cnt; } if (n > k) { return MaxValue ; } return Math.Min(solve(a * n, k, a, b, Cnt + 1), solve(b * n, k, a, b, Cnt + 1)); } static public void Main (){ int n = 4; int k = 5184; int a = 2, b = 3; // Function Call int res = solve(n, k, a, b, 0); if (res != MaxValue) Console.WriteLine(res); else Console.WriteLine(-1); } } // This code is contributed by ksam24000
Javascript
<script> // JavaScript code for the above approach // Function to find minimum steps function solve(n, k, a, b, Cnt) { if (n == k) { return Cnt; } if (n > k) { return Number.MAX_VALUE; } return Math.min(solve(a * n, k, a, b, Cnt + 1), solve(b * n, k, a, b, Cnt + 1)); } // Driver Code let n = 4; let k = 5184; let a = 2, b = 3; // Function Call let res = solve(n, k, a, b, 0); if (res != Number.MAX_VALUE) document.write(res + '</br>') else document.write(-1 + '</br>') // This code is contributed by Potta Lokesh </script>
Output
8
Time Complexity: O(2^k)
Auxiliary Space: O(2^k)
Please Login to comment...