Minimum steps to change N to 1 by changing it to 2*N or N/10 at any step
Given an integer N, find the minimum number of operations to change N to 1. If not possible, print -1. One operation is defined as either converting N to the number 2*N or converting N to the number N/10(only if N is divisible by 10).
Examples:
Input: N = 50
Output: 3
Explanation: N can be converted to 1 in 3 steps as follows:
-> Firstly, multiply N by 2 and change N from 50 to 100 (2*N)
-> Then, divide N by 10 and change N from 100 to 10 (N/10)
-> And then, divide N by 10 and change N from 10 to 1 (N/10)
Input: N = 120
Output: -1
Explanation: There is no possible way for Geek to get to position 1.
Approach: N can be converted to 1 only if N is reducible to 1 either by multiplying by 2 or dividing by 10 at each step. Now N can’t be reduced to 1 following these steps if N has a prime factor other than 2 and 5. Moreover, if the number of 2s is more than 5s in the prime factorization of N, N can’t reduce it to 1 because it will not be possible to neutralize all the 2s. Equal numbers of 2s and 5s can be neutralized by dividing the number by 10. The extra 5s can be neutralized by multiplying with extra 2s and then dividing by 10. Follow the steps below to solve the problem:
- Initialize the variables twos and fives as 0 to count the number of 2 and the number of 5 in the prime factorisation of the number N.
- Iterate in a while loop till N%2 is equal to 0 and perform the following steps:
- Divide the number N by 2.
- Increase the value of twos by 1.
- Iterate in a while loop till N%5 is equal to 0 and perform the following steps:
- Divide the number N by 5.
- Increase the value of fives by 1.
- If N is equal to 1 and the number of twos is less than equal to the number of fives, then, print 2*fives-twos as the answer.
- Else, print -1.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void check( int N)
{
int twos = 0, fives = 0;
while (N % 2 == 0) {
N /= 2;
twos++;
}
while (N % 5 == 0) {
N /= 5;
fives++;
}
if (N == 1 && twos <= fives) {
cout << 2 * fives - twos;
}
else {
cout << -1;
}
}
int main()
{
int N = 50;
check(N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void check( int N)
{
int twos = 0 , fives = 0 ;
while (N % 2 == 0 )
{
N /= 2 ;
twos++;
}
while (N % 5 == 0 )
{
N /= 5 ;
fives++;
}
if (N == 1 && twos <= fives)
{
System.out.println( 2 * fives - twos);
}
else
{
System.out.println(- 1 );
}
}
public static void main (String[] args)
{
int N = 50 ;
check(N);
}
}
|
Python3
def check(N):
twos = 0
fives = 0
while (N % 2 = = 0 ):
N / = 2
twos + = 1
while (N % 5 = = 0 ):
N / = 5
fives + = 1
if (N = = 1 and twos < = fives):
print ( 2 * fives - twos)
else :
print ( - 1 )
if __name__ = = '__main__' :
N = 50
check(N)
|
C#
using System;
class GFG{
static void check( int N)
{
int twos = 0, fives = 0;
while (N % 2 == 0) {
N /= 2;
twos++;
}
while (N % 5 == 0) {
N /= 5;
fives++;
}
if (N == 1 && twos <= fives) {
Console.Write( 2 * fives - twos);
}
else {
Console.Write(-1);
}
}
public static void Main()
{
int N = 50;
check(N);
}
}
|
Javascript
<script>
function check(N)
{
var twos = 0, fives = 0;
while (N % 2 == 0)
{
N /= 2;
twos++;
}
while (N % 5 == 0)
{
N /= 5;
fives++;
}
if (N == 1 && twos <= fives)
{
document.write(2 * fives - twos);
}
else
{
document.write(-1);
}
}
var N = 50;
check(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
21 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...