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++
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector< int > findPowers( int K)
{
vector< int > powers;
int val = 1;
while (val <= 1e18) {
powers.push_back(val);
val *= K;
}
return powers;
}
int minOperations( int N, int K)
{
vector< int > powers = findPowers(K);
int res = INT_MAX;
for ( int x = 0; x < powers.size(); x++) {
string s1 = to_string(powers[x]);
string s2 = to_string(N);
int i = 0, j = 0;
while (i < s1.size() && j < s2.size()) {
if (s1[i] == s2[j]) {
i++;
}
j++;
}
res = min(res,
( int )(s1.size()
+ s2.size() - 2 * i));
}
return res;
}
int32_t main()
{
int N = 247;
int K = 3;
cout << minOperations(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> findPowers( int K)
{
Vector<Integer> powers = new Vector<Integer>();
int val = 1 ;
while (val <= ( int )(9999999999L)) {
powers.add(val);
val *= K;
}
return powers;
}
static int minOperations( int N, int K)
{
Vector<Integer> powers = findPowers(K);
int res = Integer.MAX_VALUE;
for ( int x = 0 ; x < powers.size(); x++) {
String s1 = String.valueOf(powers.get(x));
String s2 = String.valueOf(N);
int i = 0 , j = 0 ;
while (i < s1.length() && j < s2.length()) {
if (s1.charAt(i) == s2.charAt(j)) {
i++;
}
j++;
}
res = Math.min(res,
( int )(s1.length()
+ s2.length() - 2 * i));
}
return res;
}
public static void main(String[] args)
{
int N = 247 ;
int K = 3 ;
System.out.print(minOperations(N, K));
}
}
|
Python3
import sys
def findPowers(K):
powers = []
val = 1
while (val < = 1e18 ):
powers.append(val)
val * = K
return powers
def minOperations(N, K):
powers = findPowers(K)
res = sys.maxsize
for x in range ( len (powers)):
s1 = str (powers[x])
s2 = str (N)
i = 0
j = 0
while (i < len (s1) and j < len (s2)):
if (s1[i] = = s2[j]):
i + = 1
j + = 1
res = min (res,
( int )( len (s1)
+ len (s2) - 2 * i))
return res
if __name__ = = "__main__" :
N = 247
K = 3
print (minOperations(N, K))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static List< int > findPowers( int K)
{
List< int > powers = new List< int >();
int val = 1;
while (val <= (( int )(999999999))) {
powers.Add(val);
val *= K;
}
return powers;
}
static int minOperations( int N, int K)
{
List< int > powers = findPowers(K);
int res = int .MaxValue;
for ( int x = 0; x < powers.Count; x++) {
String s1 = String.Join( "" ,powers[x]);
String s2 = String.Join( "" ,N);
int i = 0, j = 0;
while (i < s1.Length && j < s2.Length) {
if (s1[i] == s2[j]) {
i++;
}
j++;
}
res = Math.Min(res,
( int )(s1.Length
+ s2.Length - 2 * i));
}
return res;
}
public static void Main(String[] args)
{
int N = 247;
int K = 3;
Console.Write(minOperations(N, K));
}
}
|
Javascript
<script>
function findPowers(K){
var powers = []
var val = 1
while (val <= 1e18){
powers.push(val)
val *= K
}
return powers
}
function minOperations(N, K){
var powers = findPowers(K)
var res = 99999999999;
for ( var x = 0; x < powers.length;x++){
var s1 = (powers[x].toString());
var s2 = (N.toString());
var i = 0
var j = 0
while (i < s1.length && j < s2.length){
if (s1[i] == s2[j]){
i += 1
}
j += 1
}
res = Math.min(res,
(s1.length
+ s2.length - 2 * i))
}
return res
}
N = 247
K = 3
document.write(minOperations(N, K));
</script>
|
Time Complexity: O((log N)2)
Auxiliary Space: O(1)
Last Updated :
03 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...