Minimum division by 10 and multiplication by 2 required to reduce given number to 1
Given an integer N, the task is to reduce N to 1 by a minimum number of operations involving multiplication by 2 and division by 10. If 1 cannot be obtained, then print “-1”.
Examples:
Input: N = 5
Output: 2
Explanation:
Below are the operations performed:
1st operation: Multiply N by 2. Therefore, N = 5 * 2 = 10.
2nd operation: Divide N by 10. Therefore, N = 10/10 = 1.
Therefore, minimum number of operations required is 2.
Input: N = 4
Output: -1
Approach: The idea is to check prime factors of the given number M. If the given number has prime factors other than 2 and 5, then it is not possible to reduce the given number to 1 by the given operations. If the count of 2 exceeds that of 5 in its prime factors, then it is not possible to reduce N to 1 as all powers of 2 can’t be reduced.
Follow the steps below to solve the problem:
- Count the number of 2s present in prime factors of N and store it in a variable, say cnt2, and update N to N / 2cnt2.
- Count the number of 5s present in prime factors of N and store it in a variable, say cnt5, and update N to N / 5cnt5.
- After completing the above steps, if N is 1 and cnt2 ? cnt5, then the minimum number of steps required is 2 * cnt5 – cnt2.
- Otherwise, print “-1” as N can’t be reduced to 1 with the given operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumMoves( int n)
{
int cnt2 = 0, cnt5 = 0;
while (n % 2 == 0) {
n /= 2;
cnt2++;
}
while (n % 5 == 0) {
n /= 5;
cnt5++;
}
if (n == 1 && cnt2 <= cnt5) {
return 2 * cnt5 - cnt2;
}
else
return -1;
}
int main()
{
int N = 25;
cout << minimumMoves(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minimumMoves( int n)
{
int cnt2 = 0 , cnt5 = 0 ;
while (n % 2 == 0 )
{
n /= 2 ;
cnt2++;
}
while (n % 5 == 0 )
{
n /= 5 ;
cnt5++;
}
if (n == 1 && cnt2 <= cnt5)
{
return 2 * cnt5 - cnt2;
}
else
return - 1 ;
}
public static void main(String[] args)
{
int N = 25 ;
System.out.print(minimumMoves(N));
}
}
|
Python3
def minimumMoves(n):
cnt2 = 0
cnt5 = 0
while (n % 2 = = 0 ):
n / / = 2
cnt2 + = 1
while (n % 5 = = 0 ):
n / / = 5
cnt5 + = 1
if (n = = 1 and cnt2 < = cnt5):
return 2 * cnt5 - cnt2
else :
return - 1
if __name__ = = '__main__' :
N = 25
print (minimumMoves(N))
|
C#
using System;
class GFG{
static int minimumMoves( int n)
{
int cnt2 = 0, cnt5 = 0;
while (n % 2 == 0)
{
n /= 2;
cnt2++;
}
while (n % 5 == 0)
{
n /= 5;
cnt5++;
}
if (n == 1 && cnt2 <= cnt5)
{
return 2 * cnt5 - cnt2;
}
else
return -1;
}
public static void Main()
{
int N = 25;
Console.WriteLine(minimumMoves(N));
}
}
|
Javascript
<script>
function minimumMoves(n)
{
let cnt2 = 0, cnt5 = 0;
while (n % 2 == 0)
{
n /= 2;
cnt2++;
}
while (n % 5 == 0)
{
n /= 5;
cnt5++;
}
if (n == 1 && cnt2 <= cnt5)
{
return 2 * cnt5 - cnt2;
}
else
return -1;
}
let N = 25;
document.write(minimumMoves(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
26 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...