Minimize operations to convert N to a power of K by removing or appending any digit
Given a number N, the task is to find the minimum number of operations to convert the given integer into any power of K where at each operation either any of the digits can be deleted or any of the digits can be appended at the back of the integer.
Examples:
Input: N = 247, K = 3
Output: 1
Explanation: In the 1st operation, digit 4 can be removed. Hence, N = 27 which is a power of K.Input: N = 5, K = 2
Output: 2
Explanation: In the 1st operation, digit 5 can be removed and in the 2nd operation, digit 2 can be appended in the end. Hence, N = 2 which is a power of K.
Approach: The given problem can be solved by storing all the powers of K in a vector and calculating the number of operations required to convert the given integer N into the current power. Below are the steps to follow:
- Store all powers of K in vector powers.
- Now traverse through the vector powers and calculate the number of operations required to convert the given integer N into the current power which can be done as follows:
- Convert the given integers into strings s1 and s2.
- Initialize two variables i = 0 and j = 0.
- If s1[i] is equal to s2[j], increment both i and j. Otherwise, increment j only.
- The required number of operations will be S1.length + S2.length – (2 * i).
- Maintain the minimum of the required operations over all values which is the required answer.
Below is the implementation of the above approach:
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; #define int long long // Function to find all powers of // K in the range [1, 10^18] vector< int > findPowers( int K) { // Stores the powers of K vector< int > powers; int val = 1; // Loop to iterate over all // powers in the range while (val <= 1e18) { powers.push_back(val); val *= K; } // Return answer return powers; } // Function to find minimum operations to // convert given integer into a power of K int minOperations( int N, int K) { // Store all the powers of K vector< int > powers = findPowers(K); // Stores the final result int res = INT_MAX; // Loop to iterate through all // the powers of K for ( int x = 0; x < powers.size(); x++) { // Convert integers to strings // for easier comparison string s1 = to_string(powers[x]); string s2 = to_string(N); int i = 0, j = 0; // Loop to calculate operations // required to convert s1 to s2 while (i < s1.size() && j < s2.size()) { // Count no of equal character if (s1[i] == s2[j]) { i++; } // Increment j by 1 j++; } // Update res res = min(res, ( int )(s1.size() + s2.size() - 2 * i)); } // Return answer; return res; } // Driver Code int32_t main() { int N = 247; int K = 3; cout << minOperations(N, K); return 0; } |
Java
// Java code for the above approach import java.util.*; class GFG{ // Function to find all powers of // K in the range [1, 10^18] static Vector<Integer> findPowers( int K) { // Stores the powers of K Vector<Integer> powers = new Vector<Integer>(); int val = 1 ; // Loop to iterate over all // powers in the range while (val <= ( int )(9999999999L)) { powers.add(val); val *= K; } // Return answer return powers; } // Function to find minimum operations to // convert given integer into a power of K static int minOperations( int N, int K) { // Store all the powers of K Vector<Integer> powers = findPowers(K); // Stores the final result int res = Integer.MAX_VALUE; // Loop to iterate through all // the powers of K for ( int x = 0 ; x < powers.size(); x++) { // Convert integers to Strings // for easier comparison String s1 = String.valueOf(powers.get(x)); String s2 = String.valueOf(N); int i = 0 , j = 0 ; // Loop to calculate operations // required to convert s1 to s2 while (i < s1.length() && j < s2.length()) { // Count no of equal character if (s1.charAt(i) == s2.charAt(j)) { i++; } // Increment j by 1 j++; } // Update res res = Math.min(res, ( int )(s1.length() + s2.length() - 2 * i)); } // Return answer; return res; } // Driver Code public static void main(String[] args) { int N = 247 ; int K = 3 ; System.out.print(minOperations(N, K)); } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 code for the above approach import sys # Function to find all powers of # K in the range [1, 10^18] def findPowers(K): # Stores the powers of K powers = [] val = 1 # Loop to iterate over all # powers in the range while (val < = 1e18 ): powers.append(val) val * = K # Return answer return powers # Function to find minimum operations to # convert given integer into a power of K def minOperations(N, K): # Store all the powers of K powers = findPowers(K) # Stores the final result res = sys.maxsize # Loop to iterate through all # the powers of K for x in range ( len (powers)): # Convert integers to strings # for easier comparison s1 = str (powers[x]) s2 = str (N) i = 0 j = 0 # Loop to calculate operations # required to convert s1 to s2 while (i < len (s1) and j < len (s2)): # Count no of equal character if (s1[i] = = s2[j]): i + = 1 # Increment j by 1 j + = 1 # Update res res = min (res, ( int )( len (s1) + len (s2) - 2 * i)) # Return answer; return res # Driver Code if __name__ = = "__main__" : N = 247 K = 3 print (minOperations(N, K)) # This code is contributed by ukasp. |
C#
// C# code for the above approach using System; using System.Collections.Generic; public class GFG{ // Function to find all powers of // K in the range [1, 10^18] static List< int > findPowers( int K) { // Stores the powers of K List< int > powers = new List< int >(); int val = 1; // Loop to iterate over all // powers in the range while (val <= (( int )(999999999))) { powers.Add(val); val *= K; } // Return answer return powers; } // Function to find minimum operations to // convert given integer into a power of K static int minOperations( int N, int K) { // Store all the powers of K List< int > powers = findPowers(K); // Stores the readonly result int res = int .MaxValue; // Loop to iterate through all // the powers of K for ( int x = 0; x < powers.Count; x++) { // Convert integers to Strings // for easier comparison String s1 = String.Join( "" ,powers[x]); String s2 = String.Join( "" ,N); int i = 0, j = 0; // Loop to calculate operations // required to convert s1 to s2 while (i < s1.Length && j < s2.Length) { // Count no of equal character if (s1[i] == s2[j]) { i++; } // Increment j by 1 j++; } // Update res res = Math.Min(res, ( int )(s1.Length + s2.Length - 2 * i)); } // Return answer; return res; } // Driver Code public static void Main(String[] args) { int N = 247; int K = 3; Console.Write(minOperations(N, K)); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // Python 3 code for the above approach // Function to find all powers of // K in the range [1, 10^18] function findPowers(K){ // Stores the powers of K var powers = [] var val = 1 // Loop to iterate over all // powers in the range while (val <= 1e18){ powers.push(val) val *= K } // Return answer return powers } // Function to find minimum operations to // convert given integer into a power of K function minOperations(N, K){ // Store all the powers of K var powers = findPowers(K) // Stores the final result var res = 99999999999; // Loop to iterate through all // the powers of K for ( var x = 0; x < powers.length;x++){ // Convert integers to strings // for easier comparison var s1 = (powers[x].toString()); var s2 = (N.toString()); var i = 0 var j = 0 // Loop to calculate operations // required to convert s1 to s2 while (i < s1.length && j < s2.length){ // Count no of equal character if (s1[i] == s2[j]){ i += 1 } // Increment j by 1 j += 1 } // Update res res = Math.min(res, (s1.length + s2.length - 2 * i)) } // Return answer; return res } // Driver Code N = 247 K = 3 document.write(minOperations(N, K)); // This code is contributed by 29AjayKumar </script> |
1
Time Complexity: O((log N)2)
Auxiliary Space: O(1)
Please Login to comment...