Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
Last Updated :
14 Jun, 2021
Given an integer X, the task is to convert 1 into X by using the below-given operations:
- Multiply the number by 2.
- Multiply the number by 3.
- Add 1 to the number.
The task is to print the minimum number of operations needed to convert 1 into X using these three operations and also print the sequence of operations performed.
Examples:
Input: X = 5
Output:
3
1 3 4 5
Explanation:
Before any operation, X = 1
1st Operation: Multiply the number 1 by 3, hence X = 3.
2nd Operation: Add 1 to the number 3, hence X = 4.
3rd Operation: Add 1 to the number 4, hence X = 5.
Therefore, minimum 3 operations are required and the sequence is 1 3 4 5.
Input: X = 20
Output:
4
1 3 9 10 20
Explanation:
Before any operation, X = 1
1st Operation: Multiply the number 1 by 3, hence X = 3.
2nd Operation: Multiply the number 3 by 3, hence X = 9.
3rd Operation: Add 1 to the number 9, hence X = 10.
4th Operation: Multiply the number 10 by 2, hence X = 20.
Therefore, minimum 4 operations are required and the sequence is 1 3 9 10 20 .
Naive Approach: The simplest approach is to find all possible sequences of operations to convert 1 into X and return the one with the minimum number of operations.
Time Complexity: O(3N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above naive approach the idea is to use Dynamic Programming to find the minimum number of operations and then backtrack to find the required sequence of operations. Below are the steps:
- Initialize the dp[] to store the minimum number of operations for every index i to reach 1 to i.
- Now dp[X] store the minimum number of operations required to make X from 1. We will fill the dp[] array in the Bottom-Up Approach. For the any value X we can reduce in one of the below ways:
- If X is divisible by 2, then divide it by 2 and count this operation.
- If X is divisible by 3, then divide it by 3 and count this operation.
- Else subtract 1 from X.
- From the above steps, the recurrence relation formed is given by:
dp[X] = min(dp[X/2] + 1, // If X is divisible by 2
dp[X/3] + 1, // If X is divisible by 3
dp[X-1] + 1)
Base Condition:
dp[1] = 0 // As no operation required when X = 1
- After the dp[] array is created, backtrack using the values stored in the array and store the sequence in the list.
- Print the minimum operations and the sequence stored in the list.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void printMinOperations( int N)
{
int dp[N + 1];
for ( int i = 0; i < N + 1; i++)
dp[i] = N;
dp[1] = 0;
for ( int i = 2; i < N + 1; i++)
{
if (i % 2 == 0 && dp[i] > dp[i / 2] + 1)
dp[i] = dp[i / 2] + 1;
if (i % 3 == 0 && dp[i] > dp[i / 3] + 1)
dp[i] = dp[i / 3] + 1;
if (dp[i] > dp[i - 1] + 1)
dp[i] = dp[i - 1] + 1;
}
cout << dp[N] << endl;
vector< int >seq;
while (N > 1)
{
seq.push_back(N);
if (dp[N - 1] == dp[N] - 1)
N = N - 1;
else if (N % 2 == 0 &&
dp[N / 2] == dp[N] - 1)
N = N / 2;
else
N = N / 3;
}
seq.push_back(1);
for ( int i = seq.size() - 1; i >= 0; i--)
cout << seq[i] << " " ;
cout << endl;
}
int main()
{
int X = 96234;
printMinOperations(X);
}
|
Java
import java.util.*;
class GFG{
static void printMinOperations( int N)
{
int dp[] = new int [N + 1 ];
for ( int i = 0 ; i < N + 1 ; i++)
dp[i] = N;
dp[ 1 ] = 0 ;
for ( int i = 2 ; i < N + 1 ; i++)
{
if (i % 2 == 0 && dp[i] > dp[i / 2 ] + 1 )
dp[i] = dp[i / 2 ] + 1 ;
if (i % 3 == 0 && dp[i] > dp[i / 3 ] + 1 )
dp[i] = dp[i / 3 ] + 1 ;
if (dp[i] > dp[i - 1 ] + 1 )
dp[i] = dp[i - 1 ] + 1 ;
}
System.out.println(dp[N]);
Vector<Integer> seq = new Vector<Integer>();
while (N > 1 )
{
seq.add(N);
if (dp[N - 1 ] == dp[N] - 1 )
N = N - 1 ;
else if (N % 2 == 0 &&
dp[N / 2 ] == dp[N] - 1 )
N = N / 2 ;
else
N = N / 3 ;
}
seq.add( 1 );
for ( int i = seq.size() - 1 ; i >= 0 ; i--)
System.out.print(seq.get(i) + " " );
}
public static void main(String args[])
{
int X = 96234 ;
printMinOperations(X);
}
}
|
Python3
def printMinOperations(N):
dp = [N] * (N + 1 )
dp[ 1 ] = 0
for i in range ( 2 , N + 1 ):
if (i % 2 = = 0 and dp[i] > dp[i / / 2 ] + 1 ):
dp[i] = dp[i / / 2 ] + 1
if (i % 3 = = 0 and dp[i] > dp[i / / 3 ] + 1 ):
dp[i] = dp[i / / 3 ] + 1
if (dp[i] > dp[i - 1 ] + 1 ):
dp[i] = dp[i - 1 ] + 1
print (dp[N], end = "\n" )
seq = []
while (N > 1 ):
seq.append(N)
if dp[N - 1 ] = = dp[N] - 1 :
N = N - 1
elif N % 2 = = 0 and dp[N / / 2 ] = = dp[N] - 1 :
N = N / / 2
else :
N = N / / 3
seq.append( 1 )
for i in reversed (seq):
print (i, end = " " )
X = 96234
printMinOperations(X)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void printMinOperations( int N)
{
int []dp = new int [N + 1];
for ( int i = 0; i < N + 1; i++)
dp[i] = N;
dp[1] = 0;
for ( int i = 2; i < N + 1; i++)
{
if (i % 2 == 0 && dp[i] > dp[i / 2] + 1)
dp[i] = dp[i / 2] + 1;
if (i % 3 == 0 && dp[i] > dp[i / 3] + 1)
dp[i] = dp[i / 3] + 1;
if (dp[i] > dp[i - 1] + 1)
dp[i] = dp[i - 1] + 1;
}
Console.WriteLine(dp[N]);
List< int > seq = new List< int >();
while (N > 1)
{
seq.Add(N);
if (dp[N - 1] == dp[N] - 1)
N = N - 1;
else if (N % 2 == 0 &&
dp[N / 2] == dp[N] - 1)
N = N / 2;
else
N = N / 3;
}
seq.Add(1);
seq.Reverse();
foreach ( var i in seq)
{
Console.Write(i + " " );
}
}
public static void Main()
{
int X = 96234;
printMinOperations(X);
}
}
|
Javascript
<script>
function printMinOperations(N)
{
var dp = Array(N+1)
for ( var i = 0; i < N + 1; i++)
dp[i] = N;
dp[1] = 0;
for ( var i = 2; i < N + 1; i++)
{
if (i % 2 == 0 && dp[i] > dp[i / 2] + 1)
dp[i] = dp[i / 2] + 1;
if (i % 3 == 0 && dp[i] > dp[i / 3] + 1)
dp[i] = dp[i / 3] + 1;
if (dp[i] > dp[i - 1] + 1)
dp[i] = dp[i - 1] + 1;
}
document.write( dp[N] + "<br>" );
var seq = [];
while (N > 1)
{
seq.push(N);
if (dp[N - 1] == dp[N] - 1)
N = N - 1;
else if (N % 2 == 0 &&
dp[N / 2] == dp[N] - 1)
N = N / 2;
else
N = N / 3;
}
seq.push(1);
for ( var i = seq.length - 1; i >= 0; i--)
document.write( seq[i] + " " );
document.write( "<br>" );
}
var X = 96234;
printMinOperations(X);
</script>
|
Output:
14
1 3 9 10 11 33 99 297 891 2673 8019 16038 16039 48117 96234
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...